누군가 stSQL 문자열을 살펴보고 UPDATE 문과 관련된 구문 오류를 해결할 수 있습니까?VBA에서 UPDATE SQL 문을 사용하여 액세스 데이터베이스 업데이트
런타임 오류 '-2147217900 (8004e14)': UPDATE 문의 구문 오류입니다.
나는 SQL에 대한 기초적인 이해가 있으며, 어디서 잘못되었는지 이해하지 못하는 것 같습니다.
FileName UserForm 값이 Access Db의 FileName 필드와 일치하면 표 1의 필드를 업데이트하려고합니다.
감사
Public Sub UpdateDatabaseEntry()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim stDB As String, stSQL As String, stProvider As String
Dim FileName As String
Dim Nickname As String
Dim RecipientName As String
Dim RecipientRelationship As String
Dim Summary As String
Dim Noteworthy As String
Dim PreparedBy As String
FileName = UserForm1.FileNameTextBox.Text
Nickname = UserForm1.NicknameTextBox.Text
RecipientName = UserForm1.RecipientNameTextBox.Text
RecipientRelationship = UserForm1.RecipientRelationshipComboBox.Text
Summary = UserForm1.SummaryTextBox.Text
Noteworthy = UserForm1.NoteworthyCheckBox.Value
PreparedBy = UserForm1.PreparedByTextBox.Text
stDB = "Data Source= E:\MyDb.accdb"
stProvider = "Microsoft.ACE.OLEDB.12.0"
//Opening connection to database
With cn
.ConnectionString = stDB
.Provider = stProvider
.Open
End With
//SQL Statement telling database what to do
stSQL = "UPDATE Table1" & _
"SET Nickname= '" & Nickname & "', RecipientName= '" & RecipientName & "', " & _
"RecipientRelationship= '" & RecipientRelationship & "', Summary= '" & Summary & "', " & _
"Noteworthy= '" & Noteworthy & "', PreparedBy= '" & PreparedBy & "', " & _
"WHERE FileName= '" & FileName & "'"
cn.Execute stSQL
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
일반적으로 이와 같은 문제가 발생하면 SQL을 실행하기 전에'Debug.Print stSQL'을 바로 실행하십시오. 그렇게하면 직접 실행 창에서 SQL이 무엇인지 알 수 있으므로 구문 오류를 찾는 데 도움이됩니다. – Yawar