hello,<br>
<br>
i need some help, how would you put an Access SQL statement into visual basic code?, i have tried cut and paste butit wont work for some reason,<br>
<br>
thanx<br>
bye<br>
This example uses ADO, so you have to set a reference to MS ADO 2.0 or MS ADO 2.1.<br>
<br>
Private Sub Form_Load()<br>
Dim adoConn As ADODB.Connection<br>
Dim adoRecordset As ADODB.Recordset<br>
Dim strSQL As String<br>
<br>
'Create a connection object.<br>
Set adoConn = New ADODB.Connection<br>
<br>
'Connect to the GenTCA DSN (Data Source Name), where UserID and Password are blank.<br>
adoConn.Open "MyDSN", "", ""<br>
<br>
'Select the FirstName, LastName, Address, Trans_Num and SectionNum from the Consumer table.<br>
strSQL = "SELECT CON_NUMBER, FIRSTNAME, LASTNAME, ADDRESS, TRANS_NUM, SECTIONNUM FROM CONSUMER ORDER BY LASTNAME"<br>
<br>
'Create a recordset object.<br>
Set adoRecordset = New ADODB.Recordset<br>
<br>
'Create the selection set so we can now go read the values.<br>
adoRecordset.Open strSQL, adoConn, adOpenDynamic, adLockOptimistic<br>
<br>
'While we're not at the end of the recordset, keep reading in data from the table.<br>
Do While Not adoRecordset.EOF<br>
<br>
'Print each field from the table that we selected in the SQL Statement.<br>
With adoRecordset<br>
debug.print !CON_NUMBER & ""<br>
debug.print !FIRSTNAME & ""<br>
debug.print !LASTNAME & ""<br>
debug.print !ADDRESS & ""<br>
debug.print !TRANS_NUM & ""<br>
debug.print !SECTIONNUM & ""<br>
End With<br>
<br>
'Move to the next record in the recordset.<br>
adoRecordset.MoveNext<br>
Loop<br>
<br>
'Close the objects.<br>
adoRecordset.Close<br>
adoConn.Close<br>
<br>
'Release the memory our objects are using.<br>
Set adoRecordset = Nothing<br>
Set adoConn = Nothing<br>
End Sub <p>Steve Meier<br><a href=mailto:sdmeier@jcn1.com>sdmeier@jcn1.com</a><br><a href= > </a><br>
You have to call your SQL statement from a string and open a recordset.<br>
<br>
Here is a piece of code where I ran a SQL query and returned the number of resulting records. Of course, you will have to substitute your own SQL and you can use a continuation character at the end of lines instead of concatenating strings....<br>
<br>
Sub Chart9_Issued()<br>
<br>
Dim rstChart9_Issued As Recordset<br>
<br>
Set dbs = CurrentDb<br>
<br>
strSQL = "SELECT qryExportView.I_PROBLEM_NO, "<br>
strSQL = strSQL + "qryExportView.RSE_ID, qryExportView.ISSUE_DATE "<br>
strSQL = strSQL + "FROM qryExportView "<br>
strSQL = strSQL + "WHERE (((qryExportView.RSE_ID)<> ""ERROR2"" "<br>
strSQL = strSQL + "AND ((qryExportView.ISSUE_DATE)>= #" + strBegin + "# "<br>
strSQL = strSQL + "AND (qryExportView.ISSUE_DATE) < #" + strEnd + "#"<br>
strSQL = strSQL + ");"<br>
<br>
Set rstChart9_Issued = dbs.OpenRecordset(strSQL)<br>
<br>
With rstChart9_Issued<br>
If .BOF = False Then<br>
.MoveLast<br>
lngChart9_Issued = rstChart9_Issued.RecordCount<br>
Else: lngChart9_Issued = 0<br>
End If<br>
<br>
'Debug.Print lngChart9_Issued<br>
rstChart9_Issued.Close<br>
End With<br>
<br>
End Sub
are yoy working with VB5 - VB6 ? the different is the jet3.51 or jet4.0 !<br>
<br>
To mar :you can better change de + to & <br>
<br>
Eric<br>
<p>Eric De Decker<br><a href=mailto:vbg.be@vbgroup.nl>vbg.be@vbgroup.nl</a><br><a href=
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.