0

I have a form like this

When I run my program, I can't insert data because there's an error. This is when an error appear

Public cn As New ADODB.Connection
Public rs As New ADODB.Recordset

Public Sub main()
On Error GoTo eror
Set cn = New ADODB.Connection
cn.ConnectionString = "Driver={Mysql ODBC 3.51 Driver};server = 
localhost;port=3306; database=ksr; user= root; password=; option=3;"

cn.Open
If cn.State = adStateOpen Then
cn.CursorLocation = adUseClient
     MsgBox "Terhubung ke database.", vbInformation, "Informasi"
End If
LoginKSR.Show
Exit Sub
eror:
    MsgBox "Tidak dapat terhubung. Pastikan anda sudah membuka database." & Err.Description, vbCritical, "PERINGATAN!"
End Sub

Dim strsql As String
Private Sub CmdSimpan_Click()
Set rs = New Recordset
**strsql = "insert into tblstaf values('" & txtID_staf.Text & "','" & txtUID.Text & "','" & txtNama.Text & "','" & txtTTL.Text & "','" & txtAlamat & "','" & txtJenis_kelamin.Text & "','" & txtID_jabatan.Text '")**
Set rs = cn.Execute(strsql)
Staf
MsgBox "Data Telah Berhasil Disimpan.", vbInformation, "Simpan Data"
End Sub


Private Sub DataGrid1_DblClick()
On Error GoTo pesan
Dim adu As Integer
adu = DataGrid1.Row
txtID_staf.Text = DataGrid1.Columns(0).Text
txtUID.Text = DataGrid1.Columns(1).Text
txtNama.Text = DataGrid1.Columns(2).Text
txtTTL.Text = DataGrid1.Columns(3).Text
txtAlamat.Text = DataGrid1.Columns(4).Text
txtJenis_kelamin.Text = DataGrid1.Columns(5).Text
txtID_jabatan.Text = DataGrid1.Columns(6).Text

Exit Sub
pesan:
    MsgBox "Data yang anda klik kosong !", vbInformation, "Informasi"
Staf
Exit Sub
End Sub


Private Sub Staf()
Dim lihat As New Recordset
cn.CursorLocation = adUseClient
Set lihat = New Recordset
sql = "Select * From tblstaf"
lihat.Open sql, cn, adOpenStatic, adLockReadOnly
Set DataGrid1.DataSource = lihat.DataSource
End Sub

What's wrong with my program and what object that must I create? I use Visual basic 6 and MySQL.In ** is highlight when I debug.

  • Possible duplicate of [Error checking for NULL in VBScript](https://stackoverflow.com/questions/14507526/error-checking-for-null-in-vbscript) – Dezza Jul 11 '17 at 11:37

1 Answers1

0

in your sub cmdSimpan_click you have a long line of textboxes you are putting in your sql query. you should NOT do it the way you are doing it because you can hack the database by putting sql code in the textboxes. You should look at using parameters.

" & txtTTL.Text & "','" & txtAlamat & "','" & 

should read " & txtTTL.Text & "','" & txtAlamat.text & "','" &