Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if value exists in Access table

Tags:

vba

ms-access

I'm getting an error message while trying to check if a serial number already exists in my table (Access 2010).

Here's the code I'm using:

If CurrentDb.OpenRecordset("Select count(*) from Table_Name where vSerial='" & Forms!Form_Name!vSerial & "';").Fields(0) > 0 Then
MsgBox ("Serial number already in use")
Else
Me.ctrl_register.Pages(1).Enabled = True
Me.ctrl_register.Pages(1).SetFocus
End If

What am I doing wrong?

I'm using the same code with 2 different tables and I can't find the difference between them. The only difference between the tables is the number of data in them.

Could someone point me in the right direction?

The error I get is a runtime error 3464 in Access - data type missmatch in criteria expression.

The variable vSerial is defined as a number.

Thanks in advance!

like image 717
octsim Avatar asked Jan 26 '14 16:01

octsim


People also ask

How do you search for a value in access?

On the Home tab, in the Find group, click Find. The Find and Replace dialog box appears, with the Find tab selected. In the Find What box, type the value for which you want to search. To change the field that you want to search or to search the entire underlying table, click the appropriate option in the Look In list.

What is DLookup in access?

In Access desktop databases you can use the DLookup function to get the value of a particular field from a specified set of records (a domain). Use the DLookup function in a Visual Basic for Applications (VBA) module, a macro, a query expression, or a calculated control on a form or report.


1 Answers

vSerial is integer value and you have used '' in your query, remove them. Quotes are used for string values.

If CurrentDb.OpenRecordset("Select count(*) from Table_Name where vSerial=" & Forms!Form_Name!vSerial & ";").Fields(0) > 0 Then
    MsgBox ("Serial number already in use")
Else
    Me.ctrl_register.Pages(1).Enabled = True
    Me.ctrl_register.Pages(1).SetFocus
End If
like image 123
Irfan Shaikh Avatar answered Sep 27 '22 23:09

Irfan Shaikh