Using DCount function to Determine if record will be new

JonCraver

New Member
Joined
Dec 8, 2008
Messages
5
Ok. I'm trying to use VB code to determine if the value in an unbounded combo box on a form would create a new record if it were added to a table.

Background: I have a table to track orders through our warehouse. The user must begin tracking the order and then update it as it moves through the various stations in the warehouse. At the moment, i have two forms: one to BEGIN tracking an order and one to UPDATE the tracking. It is set up this way because I dont know how to check to see if the order being updated is already in the tracking table or if it needs to add a new record. This is a problem because the user has choose the right form to use based on if the order has already been tracked or not.

I'm relatively new to VB, but have used it in my database. I havent used the DCount function before. But i was thinking of something like this to solve my problem:

1) The user types the order number into an unbounded combo box in the update form. On the After Update event, code would run a DCount on the "Order_Number" key field of the Tracking Table. It would return either a "1" if the order has been tracked (i.e. already has a record in the table) or a "0" if the order has not been tracked.
2) That value (1 or 0) would then be added to a hidden text box on the form.
3) And "if/then" code would enable the different update buttons a user has access to based on whether the tracked value is 1 or 0.

The code as I've worked on it (without any kind of If/then statement) is

Dim Tracked As String
Dim OrderNum As String

OrderNum = Me.Order_Number

Tracked = DCount("Order_Number", "tbl_PT_Data_PickingTicketTracking", "Order_Number = '" & OrderNum)
Me.Hidden_Tracking_Result = Tracked

But it returns an error--Syntax error in string "order_number = '100' for example.

I dont know why this error is happening. Is this solution on the right track?
I'm sure it is clunky. But if i could figure out how to really use the DCount function to return a result, i think i could figure out ways to streamline it.
But i'm really just flying in the dark here.

Anyone have any ideas that would help me?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
What type of field is Order_Number?

Also where is the second single quote coming from?

I don't see it anywhere in what you've posted.:)
 
Upvote 0
The type of field is Text. because almost all our order numbers are numeric. But some customer orders have a letter prefix.

I'm not sure about hte second single quote. I'm having a real trouble with criteria in the DCount function. I dont think it is because of a difficult task. I just dont know how to write it. LOL
 
Upvote 0
Ok... Here is what i used and it seemed to work,

Dim Tracked As String
Dim OrderNum As String

OrderNum = Me.Order_Number

Tracked = DCount("[Order_Number]", "tbl_PT_Data_PickingTicketTracking", "[Order_Number] ='" & OrderNum & "'")
Me.Tracked_Result = Tracked

MAN... those single and double quots are hard. I need to take a class or something.
 
Upvote 0
Jon

Glad to see you seem to have sorted the problem.:)

But I don't think it anything to do with the double quotes, the single quotes were the problem, specifically the missing one.

And you could dispense with the double quotes for that part of the code by using Chr(39).
Code:
Tracked = DCount("[Order_Number]", "tbl_PT_Data_PickingTicketTracking", "[Order_Number] =" & Chr(39) & OrderNum & Chr(39))
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top