2017-07-17 25 views
-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 

enter image description here

+0

完成「不工作」是沒有問題的描述。請閱讀[問]。 – Andre

回答

0

你行

strQuery = "Update emp Set [Name]='" & Me.TextBox1.Value & "',"&[City]='" & TextBox2.Value & "',&[Dept]='" & Me.ComboBox1.Value & "'" 

嘗試更新的表emp使用這些值的所有行。我懷疑你只想更新一行。你需要添加一個WHERE子句來指定哪一行。

編輯:只是注意到該行還有一個語法錯誤。它應該閱讀

strQuery = "Update emp Set [Name]='" & Me.TextBox1.Value & "', [City]='" & TextBox2.Value & "',[Dept]='" & Me.ComboBox1.Value & "' Where " 

,然後應該與你的where子句

+0

我可以嘗試但不能工作 –

相關問題