RUn时间错误:需要对象

时间:2017-07-11 08:44:57

标签: mysql basic

I have a form like this

当我运行程序时,由于出现错误,我无法插入数据。 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

我的程序出了什么问题,我必须创建什么对象?我使用Visual Basic 6和MySQL.In **在我调试时突出显示。

1 个答案:

答案 0 :(得分:0)

在你的sub cmdSimpan_click中,你有很多文本框放在你的sql查询中。你不应该这样做,因为你可以通过将sql代码放在文本框中来破解数据库。你应该看看使用参数。

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

应阅读" & txtTTL.Text & "','" & txtAlamat.text & "','" &