tags:

views:

141

answers:

3

I have problem in the following code :

Set con = Application.CurrentProject.Connection
    Set rs = CreateObject("ADODB.Recordset")
    stSql = "SELECT * FROM [SB Items] "
    stSql = stSql & "WHERE [SBID]=" & Me![SBID] & " AND [ItemNumber]=" & intBtn
    rs.Open stSql, con, 1    ' 1 = adOpenKeyset

    ' If no item matches, report the error and exit the function.
    If (rs.EOF) Then
        MsgBox "There was an error reading the SB Items table."
        rs.Close
        Set rs = Nothing
        Set con = Nothing
        Exit Function
    End If

Since when i execute the above sql command inside stSql which resulting in only ONE row

The control is going to the error block

and message box poping out saying "There was an error reading the SB Items table"

A: 

2 Things:

  1. I noticed that you set stSQL twice, I'm guessing for reasons of line breaking. An easier way to do this is to use " _" (a space followed by an underscore). This lets you carry a command over to the next line.

  2. Change your Msgbox error from:
    MsgBox "There was an error reading the SB Items table."
    to:
    Msgbox "Error: " & err.desc

Edit: The err.desc gives you the exact error code generated. It isn't always helpful, but it's the best you can get.

PowerUser
What is the new error message giving you?
PowerUser
Good point. Also, based on the code "There was an error reading the SB Items table." is a bad message to throw here anyway since it could be shown if there simply isn't a relevant row in the table regardless of whether an error occurred.
JohnFx
it says there was an error in executing the command
This advice is wrong. Err.desc isn't going to have anything in it because there was no error thrown.
JohnFx
No it's good advise as rs.EOF simply means you're at the last record (ie: EOF) not an error as the MsgBox implies.
Mark Nold
Right. So adding err.desc on the messagebox won't show anything because there was no error there unless he turned on inline error handling which is not indicated.
JohnFx
Avoid line breaks in VBA code -- it can lead to a particular form of unrecoverable corruption. I always wished VBA had a .= append operator, so you could do strSQL .= "..." and the right side would be appended to whatever is in the variable already.
David-W-Fenton
JohnFX is right. The err.desc is only useful if an error is being thrown. Which is not the case.
PowerUser
+1  A: 

Use this code to see how many records you get back.

if rs.Supports(adApproxPosition)=true then
  i=rs.RecordCount
  MsgBox "The number of records is: " & i
end if
'
' If no item matches, report the error and exit the function.
If (rs.EOF) Then
'and on as you have it

Also, report back err.desc for more info, if there is any.

Finally, check that Con is attached to the database you think you are using!

dcpking
(sorry for the bad layout there - I'm new at this here!)
dcpking
There is no message box popping up after this
A: 

You don't have an error block, you have a check for rs.eof.

I tend to prefer checking for zero rows with

if (rs.eof) and (rs.bof)..

since this condition can only be true if the record set is empty and not just when the pointer isn't where you expect it to be or a fat cursor is still populating.

Are you sure the code isn't doing exactly what it was designed to do and showing the message when there are no rows?

Try a debug.print stSQL to get the actual query being run and try to run that directly against the database. This will confirm two things:

  1. The Query is formed like you expect it including the values you are pulling off the form
  2. The data in the table actually contains a row that should have been caught.
JohnFx
(rs.bof) made any change i have ran the query in the db it result one record
i have added rs.bof) but still no joy
When you say that you ran the query, did you use the exact value stored in stSQL or did you recreate the query manually. I'm thinking the most likely problem is that stSQL doesn't contain what you think it does by the time it gets to the rs.open line.
JohnFx
yes i given the exact value :-(
A couple more things to try:3. On the first line add "On Error goto 0" this will make it throw an error dialog if there actually is an error. (Don't leave this in after you finish troubleshooting).4. Add a debug.print stSQL on the line before rs.open and see what it prints to the debug window (ctrl-g to open the debug window)
JohnFx
no hope still same messge box
SELECT * FROM [SB Items] WHERE [SBID]=14 AND [ItemNumber]=1SELECT * FROM [SB Items] WHERE [SBID]=9 AND [ItemNumber]=6SELECT * FROM [SB Items] WHERE [SBID]=14 AND [ItemNumber]=4SELECT * FROM [SB Items] WHERE [SBID]=2 AND [ItemNumber]=1
So you are running it multiple times? I'm assuming that because you have multiple select statements in your comment. Did you try running each of these select statements separately to make sure they all return at least one row? If not, try moving the debug.print line into the IF block so it will only print the SQL for the runs that fail to return rows.
JohnFx
The reason for multiple query run is ... there are multiple forms .. when i click on a button to open a new form ..it will run the query once ..when i open another form on another button click of the new form the same function is called and the query will run it again.
So what did it print when you moved the debug.print into the if statement?
JohnFx
Does stSQL have multiple SQL statements in it? If so, that's your problem -- Jet/ACE can't process multiple statements. I'm not sure any database can process multiple SELECTS -- what would the result look like?
David-W-Fenton
This is not nultiple sql statements ..same statement is executing many time on different clicks
I'm thinking that the event is working for some of the clicks, but not others. If you move the debug.print into the IF block we can see what the SQL is only for the instances where it fails and narrow down the problem.
JohnFx
@David W. Fenton: "I'm not sure any database can process multiple SELECTS" -- sure they can e.g. SQL Server. "what would the result look like?" -- depends on your data access technology of course e.g. in ADO classic you'd use the NextRecordset method. See http://msdn.microsoft.com/en-us/library/bb221066.aspx.
onedaywhen