-
Jul 30th, 2009, 11:14 AM
#1
Thread Starter
New Member
Help with this code...please
I have this code:
Sub AccessData()
Dim sConn As String, sSQL As String, sPath, sDB As String
sPath = Application.GetOpenFilename("Access Files (*.mdb), *.mdb")
If sPath <> False Then
sPath = Left(sPath, InStrRev(sPath, "\") - 1)
sDB = Right(sPath, Len(sPath) - InStrRev("\", sPath))
sDB = Split(sDB, ".")(0) '
sConn = "ODBC;DSN=MS Access Database;"
sConn = sConn & "DBQ=" & sPath & "\" & sDB & ".mdb;"
sConn = sConn & "DefaultDir=" & sPath & ";"
sConn = sConn & "DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5"
Debug.Print sConn
sSQL = "SELECT DISTINCT "
sSQL = sSQL & " OWNERS.WELL"
sSQL = sSQL & ", OWNERS.OPERATOR"
sSQL = sSQL & ", OWNERS.SEARCHID"
sSQL = sSQL & vbLf
sSQL = sSQL & "FROM `" & sPath & "\" & sDB & "`.OWNERS OWNERS"
sSQL = sSQL & vbLf
sSQL = sSQL & "WHERE OWNERS.ID<>'1'"
sSQL = sSQL & " And OWNERS.ID Not Like '%U')"
sSQL = sSQL & vbLf
sSQL = sSQL & "ORDER BY OWNERS.ID"
With ActiveSheet.QueryTables.Add(Connection:=sConn, Destination:=Range("A1"))
.CommandText = sSQL
.Refresh BackgroundQuery:=False
End With
End If
End Sub
---------------------------------------------
When I run it prompts a window that says:
ODBC Microsoft Access Driver Login Failed, Not a valid file name.
I press OK, and then it ask me to Login, Data Source, etc... . Well I choose database, then I select the correct database... and then the error jumps to VBA.
Run-time error '1004', SQL Syntax Error.
It points at: .Refresh Backgroundquery:=False
Any thoughts??
-
Jul 30th, 2009, 12:11 PM
#2
Fanatic Member
Re: Help with this code...please
Your single quotes ` seem incorrect. Like it was pasted into Word at some point. Replace them with '.
You should probably also google "SQL injection" as the concatenated query above looks vulnerable if the file name itself is ever renamed to contain an apostrophe.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|