第一次在这里发布海报。
我正在 VBA(Access 数据库)中创建 SQL 插入语句,但当我最终准备好通过我创建的表单进行插入时,我不断收到语法错误。似乎无法弄清楚我在这个声明中出了什么问题,因为过去同样的格式对我有用。
我在论坛上寻找已经回答过该问题的人,但找不到任何合适的内容。任何帮助将不胜感激,如果您需要任何澄清信息,请随时询问。
我创建一个子程序,在单击按钮时运行,并期望将值插入到指定的表中。但是,我收到返回的语法错误。
Private Sub addAllocation_Click()
Dim strSQL, user_id As String
Dim rs As Recordset
UserID = Left(Environ("USERNAME"), 15)
If Me.newEffectiveDate = "" Or Me.newAmount = "" Then
MsgBox "Please complete all required fields"
End If
strSQL = "INSERT INTO Participant_Allocation(Transaction_ID, Participant_ID, Loan_ID, Allocation_Amount, " & _
"Effective Date, Notes, user_ID) " & _
"VALUES('" & Me.txtTransactionID & "' , '" & Me.cmbParticipantID.Column(3) & "' , '" & Me.cmbLoan & "' , '" & _
Me.newAmount & "' , '" & Me.newEffectiveDate & "' , '" & Me.newNotes & "' , '" & UserID & "')"
Debug.Print strSQL
CurrentDb.Execute strSQL
MsgBox ("Allocation has been entered.")
Set rs = Nothing
End Sub
避免笨拙的 SQL 并使用 DAO 的强大功能来获得更简洁的代码:
Private Sub addAllocation_Click()
Dim strSQL As String
Dim user_id As String
Dim rs As DAO.Recordset
If IsNull(Me!newEffectiveDate.Value) Or IsNull(Me!newAmount.Value) Then
MsgBox "Please complete all required fields."
Exit Sub
End If
UserID = Left(Environ("USERNAME"), 15)
strSQL = "Select * From Participant_Allocation"
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset, dbAppendOnly)
rs.AddNew
rs!Transaction_ID.Value = Me!txtTransactionID.Value
rs!Participant_ID.Value = Me!cmbParticipantID.Column(3)
rs!Loan_ID.Value = Me!cmbLoan.Value
rs!Allocation_Amount.Value =Me!newAmount.Value
rs![Effective Date].Value = Me!newEffectiveDate.Value
rs!Notes.Value = Me!newNotes.Value
rs!user_ID.Value = UserID
rs.Update
rs.Close
MsgBox "Allocation has been entered."
Set rs = Nothing
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)