D
dch
I am running Access2002 under Windows XP Pro. All of my
VBA is written with ADO methods.
I am trying to open a recordset in VBA. I developed the
SQL statement by creating a query and then copying the
Access generated SQL text into my VBA code. I have done
this numerous times so am familiar with what needs edited
when transfering to VBA.
This query runs fine in the query builder, but gives an
error when the recordset is opened in VBA. The code is:
Public Sub ProblemSQL()
Dim rst As New ADODB.Recordset
Dim cnn As ADODB.Connection
Dim strSQL As String
Set cnn = CurrentProject.Connection
Let strSQL = "SELECT c.IOCardName, v.IOCardVersionID "
& _
"FROM (tblUsageLocation AS u INNER JOIN tblIOCard AS c
ON u.UsageLocationID = c.Usage) " & _
"INNER JOIN tblIOCardVer AS v ON c.IOCardID =
v.IOCardType " & _
"WHERE c.IOCardDescription Like '% 2 F%' AND
u.UsageLocationName='cable';"
rst.Open strSQL, cnn, adOpenStatic, adLockOptimistic,
adCmdText
Debug.Print "IOCardName: " & rst!IOCardName
Debug.Print "IOCardVersionID: " & rst!
IOCardVersionID
End Sub
When the rst.open line executes, I receive an error as
follows:
Run-time error '-2147467259 (80004005)':
Method 'Open' of object '_Recordset" failed
I thought at first there where problems with my WHERE
syntax. I am never sure if I need to use the '*' or
the '%' as a wildcard in SQLs in VBA.
I was finally able to get the SQL to run by completely
removing the INNER JOIN structure between
tblUsageLocation and tblIOCard and then entering the
necessary info into the WHERE statement to make up for
the missing JOIN.
I realize you can't test this without knowledge of how
the tables are set up, but any ideas on why this INNER
JOIN would cause a problem in VBA but not in the query
builder?
Thanks in advance.
-dch
VBA is written with ADO methods.
I am trying to open a recordset in VBA. I developed the
SQL statement by creating a query and then copying the
Access generated SQL text into my VBA code. I have done
this numerous times so am familiar with what needs edited
when transfering to VBA.
This query runs fine in the query builder, but gives an
error when the recordset is opened in VBA. The code is:
Public Sub ProblemSQL()
Dim rst As New ADODB.Recordset
Dim cnn As ADODB.Connection
Dim strSQL As String
Set cnn = CurrentProject.Connection
Let strSQL = "SELECT c.IOCardName, v.IOCardVersionID "
& _
"FROM (tblUsageLocation AS u INNER JOIN tblIOCard AS c
ON u.UsageLocationID = c.Usage) " & _
"INNER JOIN tblIOCardVer AS v ON c.IOCardID =
v.IOCardType " & _
"WHERE c.IOCardDescription Like '% 2 F%' AND
u.UsageLocationName='cable';"
rst.Open strSQL, cnn, adOpenStatic, adLockOptimistic,
adCmdText
Debug.Print "IOCardName: " & rst!IOCardName
Debug.Print "IOCardVersionID: " & rst!
IOCardVersionID
End Sub
When the rst.open line executes, I receive an error as
follows:
Run-time error '-2147467259 (80004005)':
Method 'Open' of object '_Recordset" failed
I thought at first there where problems with my WHERE
syntax. I am never sure if I need to use the '*' or
the '%' as a wildcard in SQLs in VBA.
I was finally able to get the SQL to run by completely
removing the INNER JOIN structure between
tblUsageLocation and tblIOCard and then entering the
necessary info into the WHERE statement to make up for
the missing JOIN.
I realize you can't test this without knowledge of how
the tables are set up, but any ideas on why this INNER
JOIN would cause a problem in VBA but not in the query
builder?
Thanks in advance.
-dch