I use a command button to copy several text box data on a Form and insert as new or update record in Table ‘TProduct” using below code.
This has been working fine except when any of the fields is null then the error message “Run-Time error ‘94’: Invalid use of Null” showed and VBA stopped.
For example, if the textbox “Allergen” is null. When I click the button, the error message appeared. The debug highlighted, in yellow, the line “strAllergen = Me.[Txt_Recipe_Allergen]”.
Can anyone tell me how to modify the code to resolve this issue?
Thanks. -
Dim dbs As Database
-
Set dbs = CurrentDb
-
-
Dim strRID As String
-
Dim strRver As String
-
Dim strAllergen As String
-
Dim strDescription As String
-
-
strRID = Me.[txt_recipeID]
-
strRver = Me.[txtRecipeVer]
-
strAllergen = Me.[Txt_Recipe_Allergen]
-
strDescription = Me.[txt_Description]
-
-
-
If IsNull(DLookup("productID", "TProduct", "([productID] ='" & Me.[txt_recipeID] & "') and ([ProductVersion] = '" & Me.[txtRecipeVer] & "')")) = True Then
-
-
' Create a new record in the TProduct table:
-
-
dbs.Execute " INSERT INTO TProduct " _
-
& "(productID, ProductVersion, Description, Allergen, vendorID, VendorLocation) VALUES " _
-
& "('" & strRID & "', '" & strRver & "', '" & strDescription & "', '" & strAllergen & "', '10000', 'NYC');"
-
-
dbs.Close
-
-
MsgBox "Open ingredient to verify changes!"
-
-
Else
-
-
Dim Msg, Style, Title
-
Msg = "ID " & strRID & " and Version " & strRver & " Exist. OK to Proceed, Cancel to Cancel."
-
Style = vbOKCancel
-
Title = "Record Exist"
-
-
Response = MsgBox(Msg, Style, Title)
-
-
If Response = vbOK Then
-
-
dbs.Execute "Update TProduct SET " _
-
& "[productID] = '" & strRID & "'," _
-
& "[ProductVersion] = '" & strRver & "'," _
-
& "[Description] = '" & strDescription & "'," _
-
& "[Allergen] = '" & strAllergen & "'," _
-
& "[vendorID] = '10000'," _
-
& "[VendorLocation] = 'NYC'" _
-
& "Where [productID] = '" & Me.[txt_recipeID] & "' and [ProductVersion] = '" & Me.[txtRecipeVer] & "';"
-
dbs.Close
-
-
Else
-
Exit Sub
-
End If
-
-
MsgBox "Open ingredient to verify changes!"
-
-
End If
-
-
End Sub
-
Use the NZ function to convert the nulls to an empty string.
2 1999
Use the NZ function to convert the nulls to an empty string.
Thanks. That worked. I am still learning VBA's build-in functions.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: xazos79 |
last post by:
Hi All,
I currenly have a GridView control i'm using to update values in a
database. The directly bound fields update fine. However, there are a
couple of places where i use EditItemTemplate...
|
by: Nick |
last post by:
How do I go about setting a default value for a row when inserting a
new record with the DetailsView ? Effectively I need to access the
underlying data source and set a column to be a default value...
|
by: fniles |
last post by:
I am accessing MS Access db with tblA whose column UNABLE can have NULL
value. When I access UNABLE whose value is null, I got an error "
Run-time exception thrown : System.InvalidCastException -...
|
by: Young |
last post by:
How do I set a field to a null value in the UPDATE statement?
Eg. To update a text
UPDATE SET = 'X'
But if I want to set it to null, what is the syntax?
TIA
|
by: RvGrah |
last post by:
I have a TabelAdapter that has an int column that allows null (no
lectures please, I have my reasons). When I use intellisense to fill in
the parameters in the InserQuery method, it shows the...
|
by: molen malat |
last post by:
i have a query with 4 fields, and a form based on it. i put another 4 textbox to get criteria to filter the query. the query runs normally when all the textbox have a value (not null) but when one or...
|
by: Robert Dufour |
last post by:
I have a sub to update a record in a sql server 2000 table.
There's a field FK, which is defined to allow nulls in the table definition,
the field type is integer.
How do I write the sub's...
|
by: Leo |
last post by:
Hey, guys
Please see the code below:
/////************************************************
RegistryKey rkHive = null;
RegistryKey rk = null;
string target = "127.0.0.1";
rkHive =...
|
by: navneetkaur |
last post by:
Updation Should Reflect at same time
hi every1
i am stuck at 1 place...i want whatever updation i do it should be reflected at the same time in table(i am using table to display all records)then if...
|
by: cdavii |
last post by:
Anyone have an idea why i get null values when i import data from a ms spread sheet in a sql tbl. The table elemet(Spread sheet) in question contains alpha numerics. The sql table is populated with...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |