2017-07-11 53 views
0

I have a form like thisRUn時間錯誤:需要對象

當我運行我的程序時,由於出現錯誤,我無法插入數據。 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 **在調試時突出顯示。

+0

的[錯誤檢查在VBScript NULL]可能的複製(https://stackoverflow.com/questions/14507526/error-checking-for-null-in-vbscript) – Dezza

回答

0

在你的子cmdSimpan_click你有一長串文本框,你把你的sql查詢。你不應該這樣做,因爲你可以通過將sql代碼放入文本框來破解數據庫。你應該看看使用參數。

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

應該讀" & txtTTL.Text & "','" & txtAlamat.text & "','" &