If Value Is Null run script

Han_S_84

Registered User.
Local time
Today, 21:51
Joined
May 5, 2010
Messages
10
I will try and explain this a best possible hopefully it will make sense.
I have a form with 4 text boxes, text1, text2 text3 and text4
I have a button which adds the values in these text boxes to a table. It adds text1 and text2 as a row , then text1 and text3 and finially then text1 and text4 as a row.
Using the following code.

DoCmd.RunSQL "Insert into suppliers(supplier_id,supplier_name) Values (Text1.Value,Text2.Value);"
DoCmd.RunSQL "Insert into suppliers(supplier_id,supplier_name) Values (Text1.Value,Text3.Value);"
DoCmd.RunSQL "Insert into suppliers(supplier_id,supplier_name) Values (Text1.Value,Text4.Value);"


I would like these rows to only be added if the second text box so text2, text3 or text4 have a value. Therefore if text boxes 2 and 4 don't have values only the row with text box 1 and 3 is added to the table.
I don't really know what I am doing now but have tried with If statements and various other methods but had no joy.
 
I use the following to check for both conditions, a null or zero length string ...

Len(Nz(Me.txtControlName1, "")) = 0

So you might be able to use something like ...

Code:
If Len(Nz(Me.txtControlName1, "")) = 0 And Len(Nz(Me.txtControlName2, "")) = 0 Then ....
HTH,
-dK
 
I will try and explain this a best possible hopefully it will make sense.
I have a form with 4 text boxes, text1, text2 text3 and text4
I have a button which adds the values in these text boxes to a table. It adds text1 and text2 as a row , then text1 and text3 and finially then text1 and text4 as a row.
Using the following code.

DoCmd.RunSQL "Insert into suppliers(supplier_id,supplier_name) Values (Text1.Value,Text2.Value);"
DoCmd.RunSQL "Insert into suppliers(supplier_id,supplier_name) Values (Text1.Value,Text3.Value);"
DoCmd.RunSQL "Insert into suppliers(supplier_id,supplier_name) Values (Text1.Value,Text4.Value);"


I would like these rows to only be added if the second text box so text2, text3 or text4 have a value. Therefore if text boxes 2 and 4 don't have values only the row with text box 1 and 3 is added to the table.
I don't really know what I am doing now but have tried with If statements and various other methods but had no joy.

The issue you are dealing with involves NULL.

I suggest you watch this video tutorial to get some insight on NULL and Nz()
http://www.datapigtechnologies.com/flashfiles/nzfunction.html

You can also check for a value in a string/text box using

if Len(text2 & vbNullString) > 0 Then
'code for Has a value
else
'code for does not have a value
endif
 
Thanks the code below works perfectly.

If Len(Text2 & vbNullString) > 0 Then
DoCmd.RunSQL "Insert into suppliers(supplier_id,supplier_name) Values (Text1.Value,Text2.Value);"
End If

If Len(Text1 & vbNullString) > 0 Then
DoCmd.RunSQL "Insert into suppliers(supplier_id,supplier_name) Values (Text1.Value,Text3.Value);"
End If


Your help is much appreciated and rid of all my frustrations with Access.
 

Users who are viewing this thread

Back
Top Bottom