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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enabling a button depending on the value of a textbox

Status
Not open for further replies.

davidrsutton

Programmer
Oct 6, 2004
94
GB
Afternoon all..

Here is my scenario:

I have a form - frminvoice - which has a field on it called invoiceamount which simply stores a monetary value.

I also have a subform on here that for each row has an issueamount field (number field)

I have another textbox on the form frminvoice - Total - that keeps a running total of the issueamounts field in the subform .

Now, I would like the 'Close form' button (command34)on my form to be disabled, but enabled when frminvoice.total = frminvoice.invoiceamount

I have tried attaching the following code to the afterupdate property of the 'total' field with no luck:

If frminvoice.total.value = frminvoice.invoiceamount.value then frminvoice.command34.enabled=true else frminvoice.command34=false

Help?!

Thanks in advance,

Dave.
 
Since the total amount is on the subform, you have to refer to the subform, like

If Me.InvoiceAmount = Me.frmYourFormSub!SumAmount Then
Me.Command.Visible = False
Else
Me.Command.Visible = True
End If



Pampers [afro]
There is only one way to change a diaper - fast
 
If Me.total.value = Me.invoiceamount.value then
Me.command34.enabled = True
Else
Me.command34.enabled = False
End If

Be sure that command34 starts out disabled. Also, if this record may be revisited, be sure to include the above in the OnCurrent event (or the user would have to re-enter one of the amounts being calculated to reenable command34)

Let them hate - so long as they fear... Lucius Accius
 
How are ya davidrsutton . . .

You need to call a common routine from:
[ol][li]The OnCurrent event of the MainForm.[/li]
[li]The AfterUpdate event of [blue]invoiceamount[/blue] on the subform (update during data entry).[/li][/ol]

Calvin.gif
See Ya! . . . . . .
 
Cheers for the thoughts guys...

Pampers: Both fields are on the main form.

StrayBullett: your piece of code is almost identical to the code I posted that didn't work... and yours I'm afraid to say doesn't work either?!

Still stuck!!!

Could it be because the field 'total' on the main form is simply just referencing a textbox in the subform that is doing the sum, and not actually doing the calculation itself?! I dunno...

 
davidrsutton . . .

In a module in the modules window, copy/paste the following routine:
Code:
[blue]Public Sub CloseEnable()
   Dim frmMain As Form, frmSub As Form, ctl As Control
   
   Set frmMain = Forms![[purple][b]MainFormName[/b][/purple]]
   Set frmSub = frmMain![[purple][b]SubFormName[/b][/purple]].Form
   Set ctl = frmMain!Command34
   
   frmSub.Recalc
   frmMain.Recalc
   
   If frmMain!Total = frmMain!InvoiceAmount Then
      ctl = True
   Else
      ctl = False
   End If
   
   Set ctl = Nothing
   Set frmSub = Nothing
   Set frmMain = Nothing

End Sub[/blue]
Next . . . in the [blue]AfterUpdate event of invoiceamount[/blue] and the [blue]OnCurrent event of the mainform[/blue], copy/paste the following:
Code:
[blue]   Call CloseEnable[/blue]
Be sure to disable your other code so there's no interaction . . .

Calvin.gif
See Ya! . . . . . .
 
David,

If you setup a global variable on the form to flag if closure is possible, you may want to consider using the form_unload event to set the cancel on close. This way the user can't close the form by any method until the values are reconciled and error message can be posted to the problem.


Stix 42
Long Live Rock and Roll
Pop is for drinking
 
Hi Guys...

Cheers AceMan1... no luck with that though I'm afraid! D'oh, I'm just going to write in huge black writing across the screen "DON'T CLOSE THIS SCREEN UNLESS THOSE" NUMBERS MATCH!"

This is going to drive me mad...
 
I replicated your form, and I think that the on currect()event doesn;t work because it takes a fraction before the calculated result is on the form. If I put a small delay before executing the comparison it works fine. This is my code:

Code:
Private Sub Form_Current()
        'Wait WAITSECONDS for the next line to execute
        Dim StartTime
        Dim WAITSECONDS As Integer
        WAITSECONDS = 0.1
        
         
        StartTime = Timer
        While Timer < StartTime + WAITSECONDS
            DoEvents
         Wend
              
     If Me.Total = Me.InvoiceAmount Then
        Me.cmdInvoiceBol.Visible = True
        Else: Me.cmdInvoiceBol.Visible = False
    End If
End Sub


Pampers [afro]
Just let it go...
 
And of course put the same code in the after()event of the amount-field

Pampers [afro]
Just let it go...
 
Hi Pampers...

Thanks for that, still can't get the ****ing thing to work though! I'm wondering if it is because one of the fields I am using is simply referencing the contents of anther field on a subform, and not actually performing the calculation itself?! I dunno... I shall keep fiddling!
 
I referenced the total on the form from the total on the subform. No problem. Are you sure your reference to the subform is correct (what is the code) and did you named the fields correctly?

Pampers [afro]
Just let it go...
 
I've definately referenced the subform correctly becasue the result of the calculation is coming through onto the main form . The calculation is done in a field on the subform, then the field on the main form that displays the result of that calculation. Everything seems to be named correctly too!

I think Ill leave it for a day or 2 and come back to it and see if it cooperates then!

Cheers Pampers,

dave
 
david and pampers,

The form_current event only fires when the record is loaded. You may want to find the fields that can be changed and add the code to their afterupdate events.

Stix 42
Long Live Rock and Roll
Pop is for drinking
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top