-1
我被困在下面提到的情況指導我。我有一個用戶表單,我用excel製作。我可以嘗試執行克勞德我已經完成插入。我可以嘗試更新或刪除不工作,請給我一些想法。 這是我的插入代碼,它工作正常應用程序連接Excel工作表訪問
insert:-
Private Sub CommandButton1_Click()
Dim cn As Object
Dim strQuery As String
Dim Name As String
Dim City As String
Dim myDB As String
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
'Initialize Variables
Name = Me.TextBox1.Value
City = Me.TextBox2.Value
Dept = Me.ComboBox1.Value
myDB = "C:\Users\abc\Desktop\nis\ni2\em.accdb"
'Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0" 'For *.ACCDB Databases
.ConnectionString = myDB
.Open
MsgBox "con created"
End With
strQuery = "INSERT INTO emp ([Name], [City],[Dept]) " & _
"VALUES (""" & Name & """, """ & City & """,""" & Dept & """); "
MsgBox "success fully insert"
cn.Execute strQuery
cn.Close
Set cn = Nothing
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.ComboBox1.Value = ""
End Sub
update code:-
Dim cn As Object
Dim strQuery As String
Dim Name As String
Dim City As String
Dim myDB As String
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
'Initialize Variables
' Name = Me.TextBox1.Value
' City = Me.TextBox2.Value
' Dept = Me.ComboBox1.Value
myDB = "C:\Users\abc\Desktop\nis\ni2\em.accdb"
'Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0" 'For *.ACCDB Databases
.ConnectionString = myDB
.Open
MsgBox "con created"
End With
strQuery = "Update emp Set [Name]='" & Me.TextBox1.Value & "',"&[City]='" & TextBox2.Value & "',&[Dept]='" & Me.ComboBox1.Value & "'"
MsgBox "success fully insert"
cn.Execute strQuery
cn.Close
Set cn = Nothing
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.ComboBox1.Value = ""
End Sub
完成「不工作」是沒有問題的描述。請閱讀[問]。 – Andre