473,396 Members | 2,004 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,396 software developers and data experts.

Handling possible Null value when Insert or Update record

79 64KB
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.

Expand|Select|Wrap|Line Numbers
  1. Dim dbs As Database
  2. Set dbs = CurrentDb
  3.  
  4.  Dim strRID As String
  5.  Dim strRver As String
  6.  Dim strAllergen As String
  7.  Dim strDescription As String
  8.  
  9.  strRID = Me.[txt_recipeID]
  10.  strRver = Me.[txtRecipeVer]
  11.  strAllergen = Me.[Txt_Recipe_Allergen]
  12.  strDescription = Me.[txt_Description]
  13.  
  14.  
  15. If IsNull(DLookup("productID", "TProduct", "([productID] ='" & Me.[txt_recipeID] & "') and ([ProductVersion] = '" & Me.[txtRecipeVer] & "')")) = True Then
  16.  
  17.  ' Create a new record in the TProduct table:
  18.  
  19. dbs.Execute " INSERT INTO TProduct " _
  20.  & "(productID, ProductVersion, Description, Allergen, vendorID, VendorLocation) VALUES " _
  21.  & "('" & strRID & "', '" & strRver & "', '" & strDescription & "', '" & strAllergen & "', '10000', 'NYC');"
  22.  
  23. dbs.Close
  24.  
  25. MsgBox "Open ingredient to verify changes!"
  26.  
  27. Else
  28.  
  29. Dim Msg, Style, Title
  30. Msg = "ID " & strRID & " and Version " & strRver & " Exist. OK to Proceed, Cancel to Cancel."
  31. Style = vbOKCancel
  32. Title = "Record Exist"
  33.  
  34. Response = MsgBox(Msg, Style, Title)
  35.  
  36.   If Response = vbOK Then
  37.  
  38.      dbs.Execute "Update TProduct SET " _
  39.       & "[productID] = '" & strRID & "'," _
  40.       & "[ProductVersion] = '" & strRver & "'," _
  41.       & "[Description] = '" & strDescription & "'," _
  42.       & "[Allergen] = '" & strAllergen & "'," _
  43.       & "[vendorID] = '10000'," _
  44.       & "[VendorLocation] = 'NYC'" _
  45.       & "Where [productID] = '" & Me.[txt_recipeID] & "' and [ProductVersion] = '" & Me.[txtRecipeVer] & "';"
  46.      dbs.Close
  47.  
  48.     Else
  49.     Exit Sub
  50.     End If
  51.  
  52. MsgBox "Open ingredient to verify changes!"
  53.  
  54. End If
  55.  
  56. End Sub
  57.  
May 7 '12 #1

✓ answered by Rabbit

Use the NZ function to convert the nulls to an empty string.

2 1999
Rabbit
12,516 Expert Mod 8TB
Use the NZ function to convert the nulls to an empty string.
May 7 '12 #2
Joe Y
79 64KB
Thanks. That worked. I am still learning VBA's build-in functions.
May 8 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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...
0
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...
3
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 -...
2
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
1
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...
10
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...
2
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...
2
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 =...
16
navneetkaur
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...
5
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...
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: 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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...

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.