ms-access
  • access-vba
  • 2013-04-29 100 views 0 likes 
    0

    如何解決用VBA編寫的'UPDATE'SQL查詢?我想將'姓名'字段更新爲連接表單中的2個字段的值。我寫了這段代碼,但它不起作用,有什麼建議?在VBA中連接ACCESS

    這裏是我的代碼:

    strSQL = "UPDATE dbo.Soferi_test SET Name = '" & Me![subform_soferi].Form!numele 
                   & Me![subform_soferi].Form!prenumele & _ 
          "',Cipti = '" & Me![subform_soferi].Form!certificat_cipti & _ 
          "' WHERE IDNO = " & Me![subform_soferi].Form!IDNO 
    
    +0

    您說的「它不工作」是什麼意思?它是否會導致錯誤,或者它只是沒有達到你的目的? – 2013-04-29 10:58:51

    +0

    你想在兩個領域之間做什麼?在手中,他們直接粘在一起,沒有分隔符。你最後還是缺少'& ";'' – 2013-04-29 10:58:55

    +0

    當我點擊一個按鈕 – Sergio 2013-04-29 11:21:57

    回答

    0

    這裏是什麼,我相信你正在努力實現一個基本的輪廓。

    • 它在我看來像你的表單字段是從窗體上的子窗體引用。

    您的SQL看起來很複雜,當你把一個完整的參考對象,所以我寧願

    • 創建一些變量
    • 分配表單對象變量和
    • 引用sql中的變量。

    我認爲它使程序更具可讀性。您可以通過表單上的按鈕調用以下代碼:

    '****Update a table based on the values on a subform****' 
    'Table name: dbo.Soferi_test 
    'Form Name:  frmMain 
    'Subform Name: subform_soferi 
    
    Sub updateTable() 
    
    'Declare your variables which will be used in the sql string 
    
    Dim strSQL As String 
    Dim strNumele As String 
    Dim strPrenumele As String 
    Dim strCertificatCipti As String 
    Dim strIDNO As String 
    
    'Assign your variables to the form\subform objects 
    'You should also add some validation to check that the fields are not null 
    
    strNumele = Form_frmMain.subform_soferi!numele 
    strPrenumele = Form_frmMain.subform_soferi!prenumele 
    strCertificatCipti = Form_frmMain.subform_soferi!certificat_cipti 
    strIDNO = Form_frmMain.subform_soferi!IDNO 
    
    'Build your sql string 
    strSQL = "UPDATE dbo.Soferi_test SET Soferi_test.Name = '" & strNumele & strPrenumele & "', Cipti = '" & strCertificatCipti & "' WHERE (((Soferi_test.IDNO)='" & strIDNO & "'));" 
    
    'Execute the sql 
    CurrentDb.Execute strSQL 
    
    'You will want to put in some error handling here 
    
    End Sub 
    
    相關問題