Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with SQL Execution in VB

Status
Not open for further replies.

np3il

Programmer
Aug 15, 2002
63
0
0
US
I am trying to execute SQL commands from VB6

...
strSQL = "SELECT Count([Stores].[StoreID]) AS iTotStore FROM Stores;"
rsStores.Open strSQL, dbConn
cmdSQL.CommandText = strSQL

cmdSQL.Execute

...

This command is executing ok but it is not assigning correctly to iTotStore variable.

What am I doing wrong here ???

Thanks
np3il
 
In the code you have, you are assigning an alias to the result ie the recordset will contain a field called iToStore. The field has to be assigned to a variable. This is the code I wrote that works:

Code:
Private Sub cmdCount_Click()
    Dim rsStores As ADODB.Recordset
    Dim strSQL As String
    Dim intResult As Integer
    Set rsStores = New ADODB.Recordset
    
    rsStores.ActiveConnection = CurrentProject.Connection
    
    rsStores.Open "SELECT Count(EmployeeID) As " &_ 
                    "Result FROM tblEmployee;"
    
    intResult = rsStores!Result
    
    Me.txtNumber = intResult
    
End Sub

This should give you the result, good luck!

Dan
 
Apologies, this is written in Access VBA - but the SQL techniques are the same.

Dan
 
Dan

That work fine ...

Thanks

np3il
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top