473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Query in VB to Validate data

I'm trying to validate data entered into a form field by using a
Query. I've discovered that the validation rule is pretty useless if
you need anything other than very basic validation.

I've tried using the following After Update Code:

Dim strSQL

strSQL = "SELECT tblProducts.ProductSRP FROM tblProducts"
strSQL = & "Where (((tblProducts.ProductID)=[Forms]![frmPeople]![Cases].[Form]![ProductID]));

if strSQL > Me.CompensationAmountTextBox.Value Then

MsgBox "The amount you entered is invalid"

Me.CompensationAmountTextBox.SetFocus
The idea is that this code validates the data entered and if invalid,
displays the msg box and sends the focus back to the field that
requires a valid entry.

I've also tried to get this to work by creating a Query and
referencing the Query from a Macro or with the expression builder,
with no success.
Can anyone suggest a way to do this?

Thanks,

Fred
Nov 12 '05 #1
4 3470
Let's try the BeforeUpdate event (of the control, not the form) instead and
we'll have to use a query function instead of the query.

LookupValue = DLookup("[ProductSRP]", "tblProducts", "[ProductID]=" &
Me!Cases.Form!ProductID
If LookupValue > Me.CompensationAmountTextBox.Value Then
MsgBox "The amount you entered is invalid", vbOkOnly + vbInformation,
"Data Error"
Cancel = True
Me.CompensationAmountTextBox.Undo
End If
--
Wayne Morgan
Microsoft Access MVP
"fred" <fr**@besttechsolution.com> wrote in message
news:cc**************************@posting.google.c om...
I'm trying to validate data entered into a form field by using a
Query. I've discovered that the validation rule is pretty useless if
you need anything other than very basic validation.

I've tried using the following After Update Code:

Dim strSQL

strSQL = "SELECT tblProducts.ProductSRP FROM tblProducts"
strSQL = & "Where (((tblProducts.ProductID)=[Forms]![frmPeople]![Cases].[Form]![ProductID]));
if strSQL > Me.CompensationAmountTextBox.Value Then

MsgBox "The amount you entered is invalid"

Me.CompensationAmountTextBox.SetFocus
The idea is that this code validates the data entered and if invalid,
displays the msg box and sends the focus back to the field that
requires a valid entry.

I've also tried to get this to work by creating a Query and
referencing the Query from a Macro or with the expression builder,
with no success.
Can anyone suggest a way to do this?

Thanks,

Fred

Nov 12 '05 #2
"fred" <fr**@besttechsolution.com> wrote in message
news:cc**************************@posting.google.c om...
I'm trying to validate data entered into a form field by using a
Query. I've discovered that the validation rule is pretty useless if
you need anything other than very basic validation.

I've tried using the following After Update Code:

Dim strSQL

strSQL = "SELECT tblProducts.ProductSRP FROM tblProducts"
strSQL = & "Where (((tblProducts.ProductID)=[Forms]![frmPeople]![Cases].[Form]![ProductID]));
if strSQL > Me.CompensationAmountTextBox.Value Then


Whoa... you're testing if a STRING is greater-than the value of a TextBox?

In the above, strSQL is equal to the SQL string "SELECT..." not the result of
executing that SQL. You need to use a Recordset based on that SQL or just use a
DLookup() function. Either of those will return the value of ProductSRP from
the table. Then you can compare that to the TextBox value.

You should also be doing this in BeforeUpdate, not AfterUpdate. As the name
suggest the AfterUpdate is too late. BeforeUpdate can be cancelled to prevent
the invalid entry from ever being committed.

Actually, if you used a DLookup() there is no reason a simple Validation Rule
shouldn't work here. The rule would be something like...

<DLookup("ProductSRP", "tblProducts", "ProductID = " & ProductID & "")

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 12 '05 #3
fr**@besttechsolution.com (fred) wrote in
news:cc**************************@posting.google.c om:
I'm trying to validate data entered into a form field by using a
Query. I've discovered that the validation rule is pretty useless if
you need anything other than very basic validation.

I've tried using the following After Update Code:

Dim strSQL

strSQL = "SELECT tblProducts.ProductSRP FROM tblProducts"
strSQL = & "Where (((tblProducts.ProductID)=[Forms]![frmPeople]![Cases]. [Form]![ProductID]));
if strSQL > Me.CompensationAmountTextBox.Value Then

MsgBox "The amount you entered is invalid"

Me.CompensationAmountTextBox.SetFocus
The idea is that this code validates the data entered and if invalid,
displays the msg box and sends the focus back to the field that
requires a valid entry.

I've also tried to get this to work by creating a Query and
referencing the Query from a Macro or with the expression builder,
with no success.
Can anyone suggest a way to do this?

Thanks,

Fred


You might consider how the > operator works on strings.

You might consider what:
strSQL = & "Where (((tblProducts.ProductID)=[Forms]![frmPeople]![Cases].
[Form]![ProductID]));
does to the value stored perviously at strSQL

and you might tell us what you are trying to do, as it's not so clear.

--
Lyle
(for e-mail refer to http://ffdba.com/contacts.htm)
Nov 12 '05 #4
Thanks for the quick suggestions!

I was able to add this to a validation rule. For some reason I could
not get the syntax correct using a before update event in VB.

Thanks much for the help!

"Rick Brandt" <ri*********@hotmail.com> wrote in message news:<bs************@ID-98015.news.uni-berlin.de>...
"fred" <fr**@besttechsolution.com> wrote in message
news:cc**************************@posting.google.c om...
I'm trying to validate data entered into a form field by using a
Query. I've discovered that the validation rule is pretty useless if
you need anything other than very basic validation.

I've tried using the following After Update Code:

Dim strSQL

strSQL = "SELECT tblProducts.ProductSRP FROM tblProducts"
strSQL = & "Where

(((tblProducts.ProductID)=[Forms]![frmPeople]![Cases].[Form]![ProductID]));

if strSQL > Me.CompensationAmountTextBox.Value Then


Whoa... you're testing if a STRING is greater-than the value of a TextBox?

In the above, strSQL is equal to the SQL string "SELECT..." not the result of
executing that SQL. You need to use a Recordset based on that SQL or just use a
DLookup() function. Either of those will return the value of ProductSRP from
the table. Then you can compare that to the TextBox value.

You should also be doing this in BeforeUpdate, not AfterUpdate. As the name
suggest the AfterUpdate is too late. BeforeUpdate can be cancelled to prevent
the invalid entry from ever being committed.

Actually, if you used a DLookup() there is no reason a simple Validation Rule
shouldn't work here. The rule would be something like...

<DLookup("ProductSRP", "tblProducts", "ProductID = " & ProductID & "")

Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Russell | last post by:
I'm having a fit with a query for a range of dates. The dates are being returned from a view. The table/field that they are being selected from stores them as varchar and that same field also...
1
by: Divya Alice George | last post by:
Dear Sir, My problem is that i am required to validate a given schema against the standard DTDs provided by w3.org- namely xmlschema.dtd and datatypes.dtd. Presently, I load the given schema into...
3
by: PiGei | last post by:
Hi all, I'm trying to build a function that - providing the dbname and the query name - show the results. I don't know how to solve this problem... when I try to insert the variable into this...
2
by: dc | last post by:
i have a xml file like this: <?xml version="1.0" encoding="utf-8"?> <validate xmlns="http://tempuri.org/fieldValidate.xsd"> <field name="Short Name" type="SN" length="10"> <requiredChar...
3
by: george.lengel | last post by:
Hello experts, I have been struggling for days to solve this problem and every suggestion I find via Google does not work for me. There is probably a solution out there that will do what I want,...
4
by: UKuser | last post by:
Hi Guys, I am trying to create an editable table of a MySQL query where every field can be updated. My example script is at: http://nana46.coconia.net/test4.php however I am currently getting...
6
by: MVM | last post by:
Hi, I am attempting to run a query in MS SQL server between two tables that have a one to many relationship. The tables are linked on GID. What I want is one instance of every record from Table...
6
by: Drum2001 | last post by:
I have a database where I need to query multiple items. Is it possible to run a query based on a textbox where the information is delimited by a comma. Example: Show me all names where...
1
by: luthriaajay | last post by:
I am stuck with a tricky situation. 1)I am reading element values from an XML document mentioned below: InputDocument.xml ----------------- <?xml version="1.0" encoding="UTF-8"?> <FIXML...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.