If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > DLookup Question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-11-12, 13:21
IJOC IJOC is offline
Registered User
 
Join Date: Feb 2009
Posts: 5
DLookup Question

Question, I am trying to test within VBA using DLookup if a records exists in table called tblAllocations prior to another action taking place. For some reason I cannot get the desired results. I want to lookup if intTransactionID exist in tblAllocations, if so it should display and MsgBox otherwise it should continue with the code. For example, from my form called frmTransactions if idsTransactionID (control name) has a matching record in tblAllocations for intTransactionID (field name) then it should display MsgBox and exit the sub; otherwise continue with code.

Dim LAllocationID As Integer
If LAllocationID = DLookup("intTransactionID", "tblAllocations", "intTransactionID = " & Forms!frmTransactions!idsTransactionID) Then
MsgBox whatever will go here.
Exit Sub

Any help would be greatly appreciated.

Thanks!
Reply With Quote
  #2 (permalink)  
Old 01-11-12, 14:06
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
If it's only for testing if a record exists in a table, you don't need to use DLookup, you can use DCount:
Code:
If DCount("intTransactionID", "tblAllocations", "intTransactionID = " & Forms!frmTransactions!idsTransactionID) <> 0 Then
This frees you from having to care for Null values, which is what DLookup will return when no row matches the criteria. Your test should be:
Code:
If Not IsNull(DLookup("intTransactionID", "tblAllocations", "intTransactionID = " & Forms!frmTransactions!idsTransactionID)) Then
__________________
Have a nice day!
Reply With Quote
  #3 (permalink)  
Old 01-11-12, 16:58
IJOC IJOC is offline
Registered User
 
Join Date: Feb 2009
Posts: 5
Worked like a champ!!!

Thanks
Pat
Reply With Quote
  #4 (permalink)  
Old 01-11-12, 17:02
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
You're welcome!
__________________
Have a nice day!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On