checking for a record in another table exists (1 Viewer)

swarv

Registered User.
Local time
Today, 02:07
Joined
Dec 2, 2008
Messages
196
Hi all,

I am after the code along the lines of....

If mainpage.item1.text = the same as any record in table [main query] in fields item1 or item2 or item3 or item4.
Then - msgbox

Is this possible with a line of VBA?

Thanks

Martin
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 11:07
Joined
Jan 20, 2009
Messages
12,859
Having to seach for a single term in multiple fields suggests your data could be denormalized. Would it make sense to combine the Item fields into a single field in a related table with another field to designate the attribute that originally separated them into different fields?

If appropriate this structure is easier to query. It could be done very simply in one line with a DLookUp.

Otherwise you could generate a recordset with a query and count the records.

Derive a field that concatenates the four item fields with spaces (or some character that will not be found in the values) before and between them. Derive another that uses InStr function to test that field for the search term surrounded by spaces (or special character).

The space or special character is used to prevent the string being found as a substring in longer values.
 

SpentGeezer

Pure Noobism
Local time
Today, 11:07
Joined
Sep 16, 2010
Messages
258
yOU COULD USE THE dlOOKUP fuNCTION. i AM A NOOB, So I use a loop:
Code:
Dim strTheText
Dim RdSet
strTheText = item1.value '(item1 is a textbox or combobox??)

if len(strTheText) > 0 then
    set RdSet = currentdb.openrecordset("main query")
    rdset.movefirst
    do until rdset.EOF
        if rdset!item1 = strTheText then
            msgbox "What ever you want to say"
            exit sub
        elseif rdset!item2 = strTheText then
            msgbox "What ever you want to say"
            exit sub
        elseif rdset!item3 = strTheText then
            msgbox "What ever you want to say"
            exit sub
        elseif rdset!item4 = strTheText then
            msgbox "What ever you want to say"
            exit sub
        end if    
        rdset.movenext
    loop
else
    msgbox "Hey enter an Item1 Please!!"
    exit sub
end if
 

swarv

Registered User.
Local time
Today, 02:07
Joined
Dec 2, 2008
Messages
196
Hi spent,

thats works fine. Thanks for this. Great help. Now got to sort out Macro issue.

Thanks again
 

Users who are viewing this thread

Top Bottom