Full Version: Script Not Working.
UtterAccess Forums > Microsoft® Access > Access Forms
Chivo140
I have a form that when it is opened add a new record, the script I have to grey out certain fields, works just fine. However when I start to scroll through the records those fields that should be grayed out, are not. Why would the script not work when navigating through the records but work just fine when the form first opens?
sirluke34
Can you post the code of your Form_Load event? Also, do you have any code in other events, such as yourcontrol_LostFocus or yourcontrol_AfterUpdate?
dmhzx
One reason could be that the code is in the Form Load event, when it should be in the form Current event
Chivo140
I don't have any Form_Load events. Below is the code that I have on the form.

CODE
Private Sub Check114_AfterUpdate()
If Me.Check114 = True Then
Me.Check116 = False
End If
End Sub
Private Sub Check116_AfterUpdate()
If Me.Check116 = True Then
Me.Check114 = False
End If
End Sub
Private Sub Combo137_AfterUpdate()
If Me!Combo137 = "Exempt From Ramadan" Then
   Me!Combo139 = "Exempt"


End If
End Sub


Private Sub Combo139_AfterUpdate()
If Me!Combo139 = "Removed" Then
  Me!RemoveD.Enabled = True
   MsgBox "Please enter date of removal and reason for removal in the box below."
    Me.RemoveD.SetFocus
Else
  Me!RemoveD.Enabled = False
End If
End Sub
Private Sub Form_Current()
  If Me.NewRecord = True Then
    Me.Combo66.Enabled = False
    Me!Text53.Enabled = False
    Me!Text46.Enabled = False
    Me!Combo137.Enabled = False
    Me!RemoveD.Enabled = False
End If
End Sub

Private Sub Combo39_AfterUpdate()
If Me!Combo39 = "See Medical" Then
   Me!Combo66.Enabled = True
   Me!Text53.Enabled = True
   Me!Text46.Enabled = True
   Me!Combo137.Enabled = True
Else
   Me!Combo66.Enabled = False
   Me!Text53.Enabled = False
   Me!Text46.Enabled = False
   Me!Combo137.Enabled = False
End If

End Sub

Private Sub Command27_Click()
Dim strsearch As String
Dim Task As String
'Check if a keyword entered or not

If IsNull(Me.DOC1) Or Me.DOC1 = "" Then
   MsgBox "Please enter valid DOC Number.", vbOKOnly, "Keyword Needed"
    Me.DOC1.BackColor = vbYellow
    Me.DOC1.SetFocus
Else
    strsearch = Me.DOC1.Value
    Task = "SELECT * FROM Inmate Extended WHERE ((Number Like ""*" & DOC1 & "*""))"
   Me.RecordSource = Task
   Me.DOC1.BackColor = vbWhite

End If
End Sub
dmhzx
Those event only get fired when you change the controls. Not when you move from record to record.
If you want them checked when going from record to record, then you can call them from the on current of the form.

Though I'd be inclined to give them meaningful names first.

As it is, and as an example, in the form current put

call Combo39_AfterUpdate()

and see if that helps
Chivo140
I'm sorry, I'm not sure I understand you suggestion.
dmhzx
You have event for the combos.

Go into the onCurrent event for the form, and add in the code I posted.

Private Sub Form_Current()
If Me.NewRecord = True Then
Me.Combo66.Enabled = False
Me!Text53.Enabled = False
Me!Text46.Enabled = False
Me!Combo137.Enabled = False
Me!RemoveD.Enabled = False
End If
call Combo39_AfterUpdate()
end sub
That should get your Comb0 39 after update code running for each new record.

If that works OK, then do the same with the other combos
Chivo140
Ok that work great so thank you, however on another text box that I require the response doesn't work when I put in your code. I also need RemoveD to remain grey on a record depending on Combo139. When I tried added in
CODE
call Combo139_AfterUpdate()
but it does nothing. Below is the current code

CODE
Private Sub Combo139_AfterUpdate()
If Me!Combo139 = "Removed" Then
  Me!RemoveD.Enabled = True
   MsgBox "Please enter date of removal and reason for removal in the box below."
    Me.RemoveD.SetFocus
Else
  Me!RemoveD.Enabled = False
End If

End Sub
Chivo140
Never mind, I got it. I was trying to update the textbox instead of the combobox, thank you for your help.
dmhzx
Pleased we got you there OK. yw.gif

Now you have it working, here's a thought!
What I normally do in cases where I want checks like this,
I put them all in a form module, and call that from on currents, and each of the controls.

Re the meaningful names:
Since you're up to 139 and more already, it's probably too late for this one , but it is a LOT easier to manage an application if you have controls like

txtCheck
txtCustomer
cmdcancel
cboReliability

Than just numbers. Command29, Text167
The only controls I now leave with their Access allocated names are labels that I'm not doing anything with.
It takes a couple of extra seconds at the start, but pays dividends later. hat_tip.gif
Chivo140
I agree with you completely, however I was in too deep by the time I learned that lesson. Moring forward, I will make sure to do it right!
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.