conditional statement with recordset count

L

ll

Hi,
I am trying to check the input of my Access form, and I am wanting
visitors to input only numbers within the recordset. Below is the
code. The problem here is that no matter what number I input into the
text box (txtSearch), I get the MsgBox error identified below in the
code. Does the value in txtSearch perhaps need to be set equal to a
variable first, etc? Thanks, Louis

-----

If (Me![txtSearch]) > rst.RecordCount Then
MsgBox "Please enter a value greater than 0 (between 1 and " &
rst.RecordCount & ").", vbOKOnly, "Invalid Search Criterion!"
Me![txtSearch].SetFocus
Exit Sub
End If
 
J

Jeff Boyce

If I'm understanding your description, Access believes that txtSearch is
greater than the recordset count...

A couple possibilities occur to me:
1) your txtSearch control is unbound -- if so, I think you need to use
txtSearch.Value to 'see' the value
2) your recordset count is 0 -- you didn't describe how/where you generate
the recordset.

I'll suggest that you insert a breakpoint in your code at the "IF"
statement, then inspect the actual values of these two you are testing.

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
T

tina

well, you didn't post the complete code, but i'm guessing that after you
opened the recordset, you didn't "movelast" to ensure that the recordset is
completely populated. suggest you try that, as

rst.MoveLast

*before* you run the If statement.

hth
 
L

ll

Thanks for your help,

This is still throwing the error - I've added 'value' in
'txtSearch.value'. Is this a problem with the syntax (the
parentheses, etc)?
Thanks

------


If (Me![txtSearch.value]) > rst.RecordCount Then
MsgBox "Please enter a value greater than 0 (between 1 and " &
rst.RecordCount & ").", vbOKOnly, "Invalid Search Criterion!"
Me![txtSearch].SetFocus
Exit Sub
End If
 
K

Ken Sheridan

Louis:

You could try calling the MoveLast method of the recordset object first to
ensure its filled. However, relying on the RecordCount assumes that the
numbers in the rows are an unbroken series, which seems rather risky as it
does not allow for deletions, unless the series is recomputed after the
deletion of a row, and if the number is an autonumber, it also assumes that
the values have been generated sequentially, which cannot be guaranteed as an
autonumber is designed to ensure unique values, not necessarily sequential
ones.

If the form's underlying recordset is based on a table or saved query then
you could call the DLookup function to check for the existence of the value,
e.g.

If IsNull(DLookup("SomeField", "SomeTableOrQuery", "SomeField = " &
[txtSearch])) Then
MsgBox "Number not found.", vbExclamation, "Invalid Search Criterion!"
Me.txtSearch.SetFocus
End If

If the form's underlying recordset is based on a static SQL statement you
could establish a recordset based on the SQL statement but further restricted
to return only the row with the value entered in the relevant column, and
then test for an empty recordset, e.g.

Dim rst As ADODB.Recordset
Dim strSQL As String

strSQL = _
"SELECT * " & _
"FROM Products INNER JOIN Categories " & _
"ON Products.CategoryID = Categories.CategoryID " & _
"WHERE Category = ""Widget"" " & _
"AND ProductNumber = " & Me.txtSearch

Set rst = New ADODB.Recordset

With rst
.ActiveConnection = CurrentProject.Connection
.Open _
Source:=strSQL, _
CursorType:=adOpenForwardOnly

If .BOF And . EOF Then
MsgBox "Number not found.", vbExclamation, "Invalid Search
Criterion!"
Me.txtSearch.SetFocus
End If
End With

If the form's underlying recordset is not static, e.g. it might have been
filtered by the user and you want to search only within the filtered
recordset then you could call the FindFirst method of the form's Recordset
object:

Dim rst As Object
Set rst = Me.Recordset

rst.FindFirst "SomeField = " & Me.txtSearch
If rst.NoMatch Then
MsgBox "Number not found.", vbExclamation, "Invalid Search Criterion!"
Me.txtSearch.SetFocus
End If

Ken Sheridan
Stafford, England

ll said:
Hi,
I am trying to check the input of my Access form, and I am wanting
visitors to input only numbers within the recordset. Below is the
code. The problem here is that no matter what number I input into the
text box (txtSearch), I get the MsgBox error identified below in the
code. Does the value in txtSearch perhaps need to be set equal to a
variable first, etc? Thanks, Louis

-----

If (Me![txtSearch]) > rst.RecordCount Then
MsgBox "Please enter a value greater than 0 (between 1 and " &
rst.RecordCount & ").", vbOKOnly, "Invalid Search Criterion!"
Me![txtSearch].SetFocus
Exit Sub
End If
 
J

Jeff Boyce

Let me back up and try again...

Why? As in "why are you asking your users to input a number, and why are
you comparing that to the number of records in a recordset?"

What you've described is a "how" -- how you are trying to do something. But
you've not described the why or what.

Are you really trying to give the users a way to select a particular record?
If so, a combo box will make this much easier.

Regards

Jeff Boyce
Microsoft Office/Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Working with Filters 0
Record Search within a Form 3
Help with search code 4
DoCmd.GoToControl "[MODEL]" 1
Access Form Validation 2
Need help with this code 0
Search form help 3
Detail in form goes blank 5

Top