2014-02-16 2 views
0

누군가 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 
+0

일반적으로 이와 같은 문제가 발생하면 SQL을 실행하기 전에'Debug.Print stSQL'을 바로 실행하십시오. 그렇게하면 직접 실행 창에서 SQL이 무엇인지 알 수 있으므로 구문 오류를 찾는 데 도움이됩니다. – Yawar

답변

2

적어도 하나의 문제는 쿼리에서 공간의 부족에 의해 발생합니다. 검색어가 UPDATE Table1set으로 시작되었습니다.

stSQL = "UPDATE Table1 " & _ 
     "SET Nickname= '" & Nickname & "', RecipientName= '" & RecipientName & "', " & _ 
      "RecipientRelationship= '" & RecipientRelationship & "', Summary= '" & Summary & "', " & _ 
      "Noteworthy= '" & Noteworthy & "', PreparedBy= '" & PreparedBy & "'" & _ 
     "WHERE FileName= '" & FileName & "'" 

그래도 문제가 해결되지 않으면. 그런 다음 변수 대체 후 stSQL 값으로 질문을 편집하십시오.

EDIT :

TS 지적한 바와

다른 문제는 이전 ,where (상기 고정).

+1

문제는 'Where'앞에 쉼표가 붙어 있습니다. –

+0

@ T.S. . . . 그렇습니다, 그것은 또 다른 문제로 간주됩니다. –

+0

감사! ','앞에','WHERE' 앞에 쉼표가 실제로 내 문제였습니다. 모든 것이 작동합니다! – blahblahblah

관련 문제