View Full Version : Counting Records from VBA with SQL


Piker
01-29-2008, 07:41 PM
OK. I have a few questions all relating to the same problem. The first one is the most important because its not working at all, so if thats the only one that gets solved i will still be extremelly grateful. The others can be done a better way than i have implemented but i just dont really know how.

1. I have a form with 2 combo boxes, i want to take the key value from those combo's and use them in an SQL string to query a specific set of records from my database.
Basically i get errors, i know im calling the functions incorrectly but cannot find on the net how to fix it.

here is my code:

Private Sub DwgTypeCmb_AfterUpdate()
Dim temp1, temp2, temp3 As String
Dim rsCount As Integer
DwgNumberTxt.Value = DwgSysNoCmb.Column(1) & "-" & DwgTypeCmb.Column(1) & "-"
temp1 = DwgSysNoCmb.Column(0) 'Combo to select the System Number
temp2 = DwgTypeCmb.Column(0) 'Combo to select the Drawing Type
Dim dbase As Database
Dim rs As DAO.Recordset
Dim SQLString As String
Set dbase = CurrentDb()
SQLString = "SELECT Drawings.Sequence FROM Drawings WHERE (((Drawings.System_ID) = '" & temp1 & "') And ((Drawings.Drawing_Type) = '" & temp2 & "')) ORDER BY Drawings.Sequence DESC"
MsgBox (SQLString)
Set rs = db.OpenRecordset(SQLString)
rsCount = rs.RecordCount
If rs.EOF = False Then 'I want a drawing number 123-XX-1111
Dim i As Integer 'this should as leading zeroes if number of
temp3 = Trim(Str(rsCount)) 'records is less than 1000
For i = Len(temp3) To Length - 1
temp3 = "0" & temp3
Next
Else
temp3 = "Not found"
End If
DwgNumberTxt.Value = DwgNumberTxt.Value & temp3
End Sub

Just to explain i select the system number for the first combo, then the drawing type from the second, thats when this code should run, it queries the number of records that match both the system number and the drawing type, then creates a drawing number that is incremented by 1 from the last number used.

Sorry if my description is a bit long, but my code is pretty messy so i wanted to clear it up a bit.

ajetrumpet
01-30-2008, 09:56 PM
1. I have a form with 2 combo boxes, i want to take the key value from those combo's and use them in an SQL string to query a specific set of records from my database.
Basically i get errors, i know im calling the functions incorrectly but cannot find on the net how to fix it.First of all, I don't see any Calls in your code. The only word I see that could possibly represent a function is Length, located in this line:

For i = Len(temp3) To Length - 1

You have also only defined "temp3" specifically as a string. Temp1 and Temp2 have no specificity. Just be aware of that. What errors do you get? That would help here.

It would also help if you could use the "code" tags and tab it out so it is easier to read.

Piker
01-31-2008, 01:03 AM
This is where i get errors.

Run-time error 424; object required.

the value of temp1 comes from a combo box and is 34, and temp2 is 1.


Dim dbase As Database
Dim rs As DAO.Recordset
Dim SQLString As String
Set dbase = CurrentDb()
SQLString = "SELECT Drawings.Sequence FROM Drawings WHERE (((Drawings.System_ID) = '" & temp1 & "') And ((Drawings.Drawing_Type) = '" & temp2 & "')) ORDER BY Drawings.Sequence DESC"
Set rs = db.OpenRecordset(SQLString)
rsCount = rs.RecordCount

ajetrumpet
01-31-2008, 06:14 AM
Dim dbase As Database
Dim rs As DAO.Recordset
Dim SQLString As String
Set dbase = CurrentDb()
SQLString = "SELECT Drawings.Sequence FROM Drawings WHERE (((Drawings.System_ID) = '" & temp1 & "') And ((Drawings.Drawing_Type) = '" & temp2 & "')) ORDER BY Drawings.Sequence DESC"
Set rs = db.OpenRecordset(SQLString)
rsCount = rs.RecordCountIs this entire block highlighted? Or is there one line highlighted when you get the error?

My first guess is that the error is in the SQLstring. Notice that you have not concatenated the declared variables (temp1 and temp2). You have actually enclosed them in quotes. That is probably not the right process. The first thing I would try is this:SQLstring = "SELECT Drawings.Sequence FROM Drawings WHERE
Drawings.System_ID = " & temp1 & " AND
Drawings.Drawing_Type = " & temp2 & " ORDER BY
Drawings.Sequence DESC"If that doesn't work, please indicate which part of the block is highlighed in debug mode.

Piker
01-31-2008, 02:48 PM
Set rs = db.OpenRecordset(SQLString)

This is where i get error.
Run-time error 424; object required.

I have used a msgbox to display the values of temp1 and temp2, and they are 34 and 1, when run in a query with those values the query returns many values, but if i move my mouse over rs the tooltip says rs = nothing.

CyberLynx
01-31-2008, 03:28 PM
You have not declared the variable db my friend. You declared dbase instead.

Change Dim dbase As Database to Dim db As Database

or change:

Set rs = db.OpenRecordset(SQLString)

To

Set rs = dbase.OpenRecordset(SQLString)

.

Piker
01-31-2008, 03:32 PM
You have not declared the variable db my friend. You declared dbase instead.

Change Dim dbase As Database to Dim db As Database

.

It all seems so simple now.
Thank you very much, you have saved me alot of hair...

:):):):)

ajetrumpet
01-31-2008, 06:34 PM
(Laugh...)