Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

click event of a command button

Status
Not open for further replies.

kavya

Programmer
Feb 21, 2001
83
US
Hi I have a company form, in which there is a command button which would open a form basically a list box for the user to select different sectors the company belongs.
When there is a record already with company_id the command button works fine, but if a user by mistake choose to select to enter a new company record and before entering
if they happen to click on the add sectors command button then opens a debug window.

I am trying to code in such away when a user clicks on the command button without a new record entry it should display a error message instead the debug screen

Basically in the click event of the command button I have this code

If Me.txtCompany_ID = "" Then
MsgBox ("You have to enter a company_ID first to select the sectors")
Else
DoCmd.OpenForm "frmSectors", acNormal, , , acFormEdit, acDialog, Me.txtCompany_ID
LstSectors.Requery
DoEvents
End If

But still it is not working

Could any one help me with this

Thanks a lot

 
Try rewriting your If statement to:

If Me.txtCompany_ID Is Null Then....

Hopefully, this will do the trick for you.
 

Hi Manquis
I tried changing it to
If me.txtcompay_id is null
But still it did'nt work, can you think of something else possible which is disabling the code from running

Thanks
 
What happens if you take the () off msgbox? Try using:

MsgBox "You have to enter a company_ID first to select the sectors"

or,

if isnull(me!txtCompany_ID) then

should work also.

 
Hmmm, I thought that would do it. Your code looks correct to me except for the first "If" line. Try changing it to:

If IsNull(me.txtCompany_ID) then....

I know that sounds anal, but sometimes Access differentiates between "If x is null" and "If IsNull(x)".

If that doesn't do it, then perhaps one of the experts floating around in here can help you more than I can. I've created and maintain several Access databases, but I'm still relatively new to VB coding.



 
Although it might seem redundant, you need to check for both a Null string and an empty string (""). Try this:

If IsNull(txtCompany_ID) OR txtCompany_ID = "" then...

Microsoft made a change to the way string expressions were evaluated when Access 95 was released. This change means that you always need to check for both Null strings and empty strings separately.

HTH
Lightning
 

You can also use the NZ function.

If Nz(Me.txtCompany_ID, "") = "" Then Terry Broadbent
Please review faq183-874.

"The greatest obstacle to discovery is not ignorance -- it is the illusion of knowledge." - Daniel J Boorstin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top