views:

150

answers:

2

Hi guys, I have a piece of code that I've haphazardly written in order to run a query on_click of a form button.

Private Sub Command153_Click()
If Forms![form name1]![form name2]!Text143 <> Null Then
DoCmd.OpenQuery "InsertGroup", acNormal, acEdit
Else
End If
End Sub

I haven't written a custom if before in VBA so I think I'm probably just getting this wrong as it is literally not doing anything (No errors or anything, I assume its just not running the query). I know that the query I'm calling in works fine as if I simply get the on_click to run the query without the if, I have no problems whatsoever. I'm also referencing the correct textbox and have tried this both with and without test data in the text box.

Any help is greatly appreciated!

Regards,

Andy.

+1  A: 

You'd normally cover both null and empty string. The textbox is liable to have a value, not null which you will see if you breakpoint the code to examine the value

If Nz(Forms![form name1]![form name2]!Text143,"") <> "" Then ...
gbn
Works a charm and can't believe I didn't think of it! Thanks gbn!
Andy
+2  A: 

The usual way is to say:

 If IsNull(Forms![form name1]![form name2]!Text143) Then

I do not believe it is possible to get an empty string in an Access control, unless the value is coming from a database that allows zero-length strings. Furthermore, if you are testing for zero-length strings, you may as well cover all your bases and test for space-filled strings, so you may see in some cases:

 If Trim(Forms![form name1]![form name2]!Text143 & vbNullString ) = vbNullString Then
Remou