hi,
I have this query in an accdb file. I am looking to create this query through
VBA (CreateQueryDef), but can't seem to get correct syntax.
hmm, providing a concise and complete example would have been nice...
Option Compare Database
Option Explicit
Public Sub CreateQuery(AQueryName As String)
On Local Error Resume Next
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim errorNumber As Long
Dim sql As String
Set db = CurrentDb
Set qd = db.QueryDefs.Item(AQueryName)
errorNumber = Err.Number
On Local Error GoTo LocalError
sql = "SELECT * " & _
"FROM Countries AS Co " & _
"RIGHT JOIN Currencies AS Cu " & _
"ON Co.[Country ID] = Cu.[Country ID];"
If errorNumber = 3265 Then
' Query does not exist, create it.
db.CreateQueryDef AQueryName, sql
Else
' Otherwise modify the existing one.
qd.sql = sql
End If
db.QueryDefs.Refresh
Set qd = Nothing
Set db = Nothing
Exit Sub
LocalError:
MsgBox Err.Description, vbCritical
End Sub
btw, you should avoid
a) using special characters in field and/orobject names.
b) naming tables using pluralization.
mfG
--> stefan <--