VBA访问更新和添加表单还有许多ID字段和共享字段

时间:2017-09-25 18:25:14

标签: vba ms-access access-vba

我正在尝试创建一个更新数据库的表单。有多个Act ID可以与10B关联,但它们具有共享字段(客户帐号)。在表单中有一个10B编号,将添加并用于填充未绑定的文本框。我认为我有一个良好的开端,但是在使用“我”编译的任何内容上都会出错。基本上我希望能够提取所提供的10B号码的所有记录,并根据需要进行更新或添加。任何人都能帮我解决我做错的事吗?感谢。

Const MaxActIDCount = 9
Const ActIDPrefix = "ActID"
Sub ClearTextBoxes()
'  Dim I As Integer
' For I = 1 To MaxActIDCount
' Me.Controls(ActIDPrefix & I).Value = Null
 'Next I

Dim ctl As Control

For Each ctl In Me
    If ctl.ControlType = acTextBox Then
    ctl = Null
    End If
Next
End Sub

Public Function BuildWhereClause() As String
  BuildWhereClause = " WHERE [10B_Number]=" & SearchNumberBox.Value
End Function

Sub PullInfoToTextBoxes()
  Dim RS As DAO.Recordset, SQL As String
  SQL = "SELECT [10B].Act_Party_ID, , [10B].[Customer_Account_Number(s)] FROM 10B" + BuildWhereClause
  Set RS = CurrentDb.OpenRecordset(SQL, dbForwardOnly)
  ClearTextBoxes
  While Not RS.EOF
    CustomerAccountsTextbox.Value = RS!Customer_Accounts
    For I = 1 To MaxActIDCount
    Me.Controls(ActIDPrefix & I).Value = RS!ActID
    RS.MoveNext
    Next I
  Wend
End Sub


Sub UpdateDatabase()
  Dim DB As DAO.Database, I As Integer, WhereClause As String, SQL As String
  Set DB = CurrentDb
  For I = 1 To MaxActIDCount
    WhereClause = BuildWhereClause + " AND ActID = " & I
    If IsNull(Me.Controls(ActID1TextBoxPrefix & I).Value) Then
      SQL = "DELETE FROM 10B" + WhereClause
    ElseIf DB.OpenRecordset("SELECT 1 FROM 10B" + WhereClause).EOF Then
      SQL = "INSERT INTO 10B(Act_Party_ID, [Customer_Account_Number(s)]" + _
            "VALUES (" & Me.Controls(ActID1TextBoxPrefix & I).Value & "," & CustomerAccountsTextbox.Value & ","")"
    Else
      SQL = "UPDATE 10B SET [10B].[Customer_Account_Number(s)] = " & CustomerAccountsTextbox.Value & WhereClause
    End If
    DB.Execute SQL
  Next I
End Sub

Private Sub Command67_Click()
  Dim RS As DAO.Recordset, SQL As String
  SQL = "SELECT [10B].Act_PARTY_ID, [10B].[Customer_Account_Number(s)]FROM 10B" + BuildWhereClause
  Set RS = CurrentDb.OpenRecordset(SQL, dbForwardOnly)
  DoCmd.RunMacro "ClearTextBoxes"
  While Not RS.EOF
    CustomerAccountsTXTBOX.Value = RS![Customer_Account_Number(s)]
    For I = 1 To MaxActIDCount
    Me.Controls(ActIDPrefix & I).Value = RS!Act_Party_ID
    RS.MoveNext
    Next I
  Wend
End Sub

0 个答案:

没有答案