Video : Input Data menggunakan User Form
Tutorial Level 6
Kode program 1 pada module:
Sub PanggilFormInput()
UserForm1.Show
End Sub
Kode program pada Tombol CANCEL:
Private Sub CommandButton1_Click()
Unload Me
End Sub
Kode program pada Tombol SAVE:
Private Sub CommandButton2_Click()
Simpan
End Sub
Kode Program 2 pada module:
Sub Simpan()
Dim LokasiDB As String
Dim Nomr As Long
Dim Nama As String
Dim Alamat As String
Dim Keterangan As String
Dim WB1 As Workbook
Dim WB2 As Workbook
Dim WS1_1 As Worksheet
Dim WS2_1 As Worksheet
Dim BarAkhr As Long
Set WB1 = ThisWorkbook
Set WS1_1 = WB1.Worksheets("Form")
With WS1_1
LokasiDB = .Cells(6, 7).Value
Nomr = UserForm1.TextBox1.Value
Nama = UserForm1.TextBox2.Value
Alamat = UserForm1.TextBox3.Value
Keterangan = UserForm1.TextBox4.Value
If IsEmpty(Nomr) Or Nama = "" Or Alamat = "" Then
MsgBox "Lengkapi input form!"
Exit Sub
End If
End With
Set WB2 = Workbooks.Open(LokasiDB)
Set WS2_1 = WB2.Worksheets("Database")
With WS2_1
BarAkhr = .Cells(.Cells.Rows.Count, 1).End(xlUp).Row
.Cells(BarAkhr + 1, 1).Value = Nomr
.Cells(BarAkhr + 1, 2).Value = Nama
.Cells(BarAkhr + 1, 3).Value = Alamat
.Cells(BarAkhr + 1, 4).Value = Keterangan
End With
Application.DisplayAlerts = False
WB2.Close SaveChanges:=True
Application.DisplayAlerts = True
MsgBox "Data telah disimpan.."
End Sub