what event to tie this code to so it works? (1 Viewer)

eee333

Registered User.
Local time
Yesterday, 21:42
Joined
Jan 11, 2016
Messages
36
I have this piece of code that works, but for the life of me I can't figure out what event/module to put it under. In my form, a user generates a number by filling out an unbound form and clicking a command button. The command button then sets the value of InsightId through a calculation. This is not the problem. Then, what I want to happen is, have a disabled textbox (Country) be populated depending on the values. The problem is, I cannot figure out what event/module to put the code under. I tried InsightId_Afterupdate, and the code works if you actually go in and manually change anything, but not if you generate the number with the form (the way 99% of users will be using it). I tried InsightId_Change, and same deal. I tried including it in the code that generates the InsightId in the unbound form, but for some reason that does not work for me--I think because it's both setting the value of InsightId and setting Country by the value of InsightId (I don't know why that would be the case, but it appears to be the case).

I guess worst case scenario I could just tie it to some arbitrary event, like the form having any information be entered, or something like that, but that seems messy/a bad habit to get into.

So, the short of it is, I want to find an event that takes place once InsightId is generated by pressing a command button, and AfterUpdate and Change don't seem to be working. What event can I tie it to?

I don't think any of my code is the issue since it's works perfectly fine when the changes are happening manually, just not when it's generated by VBA.

the associated code is below
Code:
'generating the Insight Id'
Private Sub Command4_Click()
If IsNull(CommID) Then
    Beep
    MsgBox "you cannot leave community blank", vbOK
    Exit Sub
Else: Forms![test lines]![InsightID] = NewInsightID()
End If
DoCmd.Close acForm, "Generator"
Beep
MsgBox "Please write down the Insight ID and your initials on the paper survey before proceeding"
Forms![test lines]![InsightID].Enabled = True
Forms![test lines]![q1].SetFocus
End Sub

'the part that I thought worked and i just need to stick somewhere'
Forms![test lines]![Country] = Baseball()

Public Function Baseball() As String
Select Case InsightID
Case 1 To 199
    Billio = "United States"
Case 200 To 299
    Billio = "Spain"
Case 330 To 666
    Billio = "Mexico"
End Select
Baseball = Billio
End Function

Weirdly, if I do in any of those places
Code:
 Forms![test lines]![Country] = "Baseball"
it inputs the word "baseball" in just fine, so maybe it is a problem with my code? But again, since it works elsewhere I think it has to do with me just not getting where/when to place it so it works properly
 
Last edited:

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 18:42
Joined
Aug 30, 2003
Messages
36,125
As you've found, the update and change events don't fire if a value is changed programatically. How about from the same event that populates the first textbox?
 

eee333

Registered User.
Local time
Yesterday, 21:42
Joined
Jan 11, 2016
Messages
36
I felt fairly certain that this fix would work, and yet, it doesn't seem to be? It still won't update it at the time like it will for the InsightID.

Code:
'part where it sets the values in the other form'
Else: Forms![test lines]![Country] = Baseball()
    Forms![test lines]![InsightID] = NewInsightID()

' truncated case select part
Public Function Baseball() As String
Select Case CommID
Case 1
    Billio = "United States"
Case 5
    Billio = "Spain"
Case 17
    Billio = "Mexico"
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 18:42
Joined
Aug 30, 2003
Messages
36,125
Where does CommID come from? Previously it was the other.
 

eee333

Registered User.
Local time
Yesterday, 21:42
Joined
Jan 11, 2016
Messages
36
Ugh, actually, the culprit was Monday morning brain--forgot that all values in Comm Id are in the 900's--it just wasn't working because my case select statement was looking for 1, not 901. It works perfectly now, thanks!
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 18:42
Joined
Aug 30, 2003
Messages
36,125
Glad to help!
 

Users who are viewing this thread

Top Bottom