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!

Do While and Loop - cmdbutton disabled or enabled depending on txtbox

Status
Not open for further replies.

wz

Programmer
Feb 16, 2001
88
US
I wish to have a cmdbutton (cmdAddNewEntry) disabled when the NextNum = 1 (NextNum is a textbox on the current form) and enabled when NextNum <>1.

Below is my code & I am not sure why it keeps locking up on me.

Private Sub Form_Current()

Do
Do While Me.NextNum = 1
If Me.NextNum = 1 Then
Me.cmdAddNewEntry.Enabled = False
Else
Me.cmdAddNewEntry.Enabled = True
Exit Do
End If
Loop
Loop Until Me.NextNum <> 1
 
How are ya wz . . .

Don't know why your looping ... all you need is:
Code:
[blue]   Me.cmdAddNewEntry.Enabled = (Me.NextNum <> 1)[/blue]
Include the above line of code in the [blue]After Update[/blue] event of [blue]NextNum[/blue] if you want to track changes in this control.

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
wz said:
I am not sure why it keeps locking up on me.
While code is running you no longer have control of the interface.
As soon as you go to any record where NextNum <> 1 you will get into a loop which you cannot get out of since you won't have any chance to change NextNum.

Don't use loops to "wait for something to happen". VBA is event driven, put code in the event(s) where the thing you are waiting for might happen. In this case I recommend creating a subroutine something like:

Code:
Private Sub SetAddNewEntryButton()
    If Me.NextNum = 1 Then
        Me.cmdAddNewEntry.Enabled = False
    Else
        Me.cmdAddNewEntry.Enabled = True
    End If
End Sub

Then call that procedure in the events where NextNum might change. Form_Current and NextNum_AfterUpdate are the two events that I can think of.
 
AceMan1: Thanks so much for your reply.

more info...
Me.NextNum is actually a txtbox that is calculated =Max([ControlNUM])+1. I am having trouble with some end users clicking on the command button cmdAddNewEntry BEFORE the NextNum has calculated (some endusers click on cmdAddNewEntry right away). It takes but 2 seconds to calculate & some users are getting a control number of 1 (since the default value is 0) and I need the number to continue to grow from a larger series. I haven't used autonum because the database is based on 3 different series of numbers-depending on the dept..., etc.

Should I be doing something completely different? like how I am calculating NextNum (on property sheet, data, controlsource?

JoeatWork
sill not working the way I wish it would. I do use similar code on other forms-see my notes above...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top