Discussion Locked

Access VBA: search returning True/False

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Two Access2000 problems:

1)
I need to be able to search for a particular record in a table from a user-interface form. The search needs to take a field value as the parameter, and return true or false. It MUST NOT visibly open the table, and does not need to actually go to or return the record. I just need to know if a record with the specified primary key (the parameter) exists.

The entire thing must be done in code, and must be invisible to the user.

2) Is there a way to either suppress or alter Access 2000 message boxes? In particular, I keep getting a "Are you sure you want to append..." message, and I want to either change the prompt, or get rid of the message completely. Can this be done, and if so, how?

Any ideas?

8 replies

by sl-campbell

For question #1:

You can write a Select statement like one of the following:

Public Function pf_bRecordExists(ByVal sTableName As String, ByVal sFieldName As String, ByVal sFindRecord As String) As Integer

Dim rs As RecordSet
Dim sqlString As String

sqlString = "SELECT Count(*) AS RecordCount FROM " & sTableName & " WHERE " & sFieldName & "='" & sFindRecord & "'"

'(The Sql this way is faster than using Distinct, or whatever - and it is optimized)

'Do your open recordset here(something like this)
Set rs = Application.CurrentDb.OpenRecordset(sqlString, dbOpenSnapshot)

pf_bRecordExists = rs("RecordCount")

rs.Close
Set rs = Nothing

End Function

Then call it like this:

Public Sub Test()
Dim bRecordExists As Boolean

bRecordExists = pf_bRecordExists("myTableName", "myFieldName", "myRecordToFind")

'Here you could also just write:
'If pf_bRecordExists("myTableName", "myFieldName", "myRecordToFind") Then

If bRecordExists Then

'Reaction Code
'Here, a message box for testing, (remove this after testing)

MsgBox "Record Exists"

Else

MsgBox "No Records"
End IF
End Sub

Using a return value type as an Integer is better, AND you can use the same function to get a record count for another task.

For question #2:
In code use DoCmd.SetWarnings = False
This can also be done in a macro command.

by sl-campbell

Just a tip: You CAN use a field name with Count as in Count(myField). However, when using Rushmore optimizable expressions, Count(*) is highly optimized as apposed to Count(myField). Besides that, the Count(myField) will NOT return a record as part of the count if myField contains a Null. Count(*) will. This is also true with using the Access DCount. However, I prefer to use my own functions so that code can be easily moved between Access'VBA and VB, or whatever. But if this isn't the case, and the db is not that large, then there is nothing wrong with strictly using VBA functions, even though some are slower (like the DCount is 20 - 25% slower than the code above - only noticable with a very large amount of records), but will serve most purposes very well.

by bob sellman

1. Call the field the user enters the value (let's assume it's text) as txtFind and call txtResult as the field to display Yes or No depending whether the value exists or not. You can do one of two things:
a. On the onUpdate property of txtFind:

Dim lngCount as long

lngCount = DCount("[anyfield]","[tablename]","[thisfield]='" & me.txtFind & "'")

if lngCount > 0 then
me.txtResult = "Yes"
else
me.txtResult = "No"
end if

**note that if txtFind is a date it should be surrounded with #, if a number, then use nothing (no # or ').

b. Set txtResult record source as the following (which you must use the builder to create..note the sytax will be slightly different than above and you don't use the single quotes or pound signs):

DLookup("[anyfield]","[tablename]","[thisfield]= me.txtFind")

Then on the onUpdate property of me.txtFind, just put the following statement:

me.txtResult.requery

2) As mentioned in the first answer you can set warnings off. You can also (on the user's desktop) go to Tools, Options, then select Edit/Find and uncheck the three boxes under confirm.

by timfox

Qu1. How about this one:

Dim dbs as database
Dim rst as recordset

set dbs = currentdb
set rst = dbs.openRecordset("SELECT primary_key FROM this_table WHERE primary_key = this_value")

if rst.recordCount <> 0 then
msgbox "record exists"
else
msgbox "record doesn't exist"
end if

rst.close
rst.close

Qu2. I doubt you can alter these messages - but as previously mentioned you can turn these off and create your own messages and acting on user responses using:

Dim response asinteger
response = msgbox ("Do you want to...?",vbquestion + vbYesNo,"Confirm")
if response = 6 then
' do something for yes
else
' do something for no
end if

This discussion is closed to new replies.