Problem running a query

  • Thread starter קובץ
  • Start date
×

קובץ

hello!

I'm trying to run the following query, but I get an error message:

The code:

Dim oDataBase As DAO.Database
Set oDataBase = OpenDatabase("C:\Program Files\Microsoft Visual
Studio\VB98\NWIND.MDB")

sql = sql & "SELECT Customers.Customers_ID, Customers.Company_ID,
Customers.C_Email, Customers.C_FName, Customers.C_LName "
sql = sql & "FROM Customers "
sql = sql & "WHERE (((Customers.Company_ID)=" & num & ")) "
sql = sql & "ORDER BY Customers.C_LName;"

Dim qdf As DAO.QueryDef
Set qdf = oDataBase.CreateQueryDef("My sql", sql)


The error message:
object variable or with block variable not set

How can I fix the problem?

thanks,
Yonina.
 
D

Duane Hookom

I don't see where you Dim sql. I tried the following and had no issues. In
my test, the CustomerID is a string, not numeric.
Sub TestIt()
Dim num As String
Dim SQL As String
num = "BERGS"
Dim oDataBase As DAO.Database
Set oDataBase = OpenDatabase("C:\Program Files\Microsoft
Office\Office10\Samples\Northwind.mdb")

SQL = SQL & "SELECT Customers.CustomerID, Customers.CompanyName,
Customers.City, Customers.ContactName "
SQL = SQL & "FROM Customers "
SQL = SQL & "WHERE (((Customers.CustomerID)='" & num & "')) "
SQL = SQL & "ORDER BY Customers.ContactName;"

Dim qdf As DAO.QueryDef
Set qdf = oDataBase.CreateQueryDef("My sql", SQL)
End Sub
 
Top