访问vba:更新表单

时间:2017-08-28 08:10:47

标签: vba access-vba ms-access-2010

我目前正在尝试为多个Acces数据库创建一个日志记录工具。 每个Acces数据库看起来都一样,并且包含相同类型的字段。

目标:数据库A,B,C中的所有记录都应该在同一个A,B,C数据库中更新,并在创建时发送到数据库X.

在数据库A,B或C中编辑记录时,它也应在数据库X中自动更新。 在数据库X中编辑记录时,它也应该在数据库A,B或C中自动更新(取决于记录所属的数据库)。

欢迎所有帮助, 提前谢谢。

下面,您可以看到我到目前为止的代码。 另请参阅以下我需要的设置图片: Insert/update between Acces Databases

Option Compare Database

Private Sub cmdAdd_Click()

    'Click on button Add gives 2 options
    '1. insert
    '2. update

    '1. insert if new ID:
    If Me.txtID.Tag & "" = "" Then
    'Data toevoegen aan tabel & BSO tabel
    Dim dbsCurrent As Database, dbsBSOlogging As Database
    Dim rs As DAO.Recordset

    Set dbsCurrent = CurrentDb
    Set dbsBSOlogging = DBEngine.Workspaces(0).OpenDatabase("BSOlogging.accdb")
    Set rs = dbsCurrent.OpenRecordset("logging")

    dbsBSOlogging.Execute "INSERT INTO loggingBSO(stdid, stdname, gender, phone, address) " & _
        " VALUES(" & Me.txtID & ",'" & Me.txtName & "','" & _
        Me.cboGender & "','" & Me.txtPhone & "','" & Me.txtAddress & "')"

    CurrentDb.Execute "INSERT INTO logging(stdid, stdname, gender, phone, address) " & _
        " VALUES(" & Me.txtID & ",'" & Me.txtName & "','" & _
        Me.cboGender & "','" & Me.txtPhone & "','" & Me.txtAddress & "')"
    Else

    Set dbsCurrent = CurrentDb
    Set rs = CurrentDb.OpenRecordset("logging")

   'update statement for current DB:

   'update data in current table'

  If rs("stdid").Value = [txtID] Then

  If rs("stdname").Value <> [txtName] Then
  rs.Edit
  rs("stdname").Value = [txtName]
  rs.Update
  End If

  If rs("gender").Value <> [cboGender] Then
  rs.Edit
  rs("gender").Value = [cboGender]
  rs.Update
  End If

  If rs("phone").Value <> [txtPhone] Then
  rs.Edit
  rs("phone").Value = [txtPhone]
  rs.Update
  End If

  If rs("address").Value <> [txtAddress] Then
  rs.Edit
  rs("address").Value = [txtAddress]
  rs.Update
  End If
  End If

   'update statement for operation DB:
     Set dbsBSOlogging = DBEngine.Workspaces(0).OpenDatabase("BSOlogging.accdb")
     Set rs = dbsBSOlogging.OpenRecordset("loggingBSO")

   'update data in table'

  If rs("stdid").Value = [txtID] Then

  If rs("stdname").Value <> [txtName] Then
  rs.Edit
  rs("stdname").Value = [txtName]
  rs.Update
  End If

  If rs("gender").Value <> [cboGender] Then
  rs.Edit
  rs("gender").Value = [cboGender]
  rs.Update
  End If

  If rs("phone").Value <> [txtPhone] Then
  rs.Edit
  rs("phone").Value = [txtPhone]
  rs.Update
  End If

  If rs("address").Value <> [txtAddress] Then
  rs.Edit
  rs("address").Value = [txtAddress]
  rs.Update
  End If
  End If


    End If

    'Clear form
    cmdClear_Click

    'Data refresh in list on form
    frmStudentSub.Form.Requery

End Sub

Private Sub cmdClear_Click()
    Me.txtID = ""
    Me.txtName = ""
    Me.txtAddress = ""
    Me.txtPhone = ""
    Me.cboGender = ""

    'focus op ID text box
    Me.txtID.SetFocus
    'Set button Edit to enable
    Me.cmdEdit.Enabled = True
    'Change caption of button Add
    Me.cmdAdd.Caption = "Add"
    'Clear tag on txtID for reset new
    Me.txtID.Tag = ""
End Sub

Private Sub cmdClose_Click()
    DoCmd.Close
End Sub

Private Sub cmdEdit_Click()
    'Check if there exists data in list
    If Not (Me.frmStudentSub.Form.Recordset.EOF And Me.frmStudentSub.Form.Recordset.BOF) Then
    'Get data to textbox control
    With Me.frmStudentSub.Form.Recordset
        Me.txtID = .Fields("stdid")
        Me.txtName = .Fields("stdname")
        Me.cboGender = .Fields("gender")
        Me.txtAddress = .Fields("address")
        Me.txtPhone = .Fields("phone")
        'Store id in Tag of txtID in case id is modified.
        Me.txtID.Tag = .Fields("stdid")
        'change caption button Add to Update.
        Me.cmdAdd.Caption = "Update"
        'Disable button edit
        Me.cmdEdit.Enabled = False

        End With
    End If
End Sub

0 个答案:

没有答案