2

I have an unbound textbox in form f_FeuilleBleue. In my code, I give it a certain value;

Debug.Print strAHNS '00 0AA 00-100 F TX-01
Form_f_FeuilleBleue.txt_AHNS = strAHNS

If I put a stopping point on the next line, the immediate window shows that

?Form_f_FeuilleBleue.txt_AHNS
 answer: 00 0AA 00-100 F TX-01

However, I still see it as blank in my form. There is no data to be read!

How do I fix this? Is it an issue with screen updating? (I have nothing setting it to off) Maybe form updating? (I have a msgBox in the BeforeUpdate event but it doesn't go in that event)

EDIT - additional info:

When I open the form, there is no problem. I can change the value in the form or by code. However the issue only happens when the form is opened from a menu-style form. Code below. Even after the opening sub finishes, the textbox won't update (visually - it does in value). After testing I see that the Change and the Update event are NOT launched when changing the value of the textbox from another sub (Private subs may be the cause?) But why is it continuing to not show the values even after the subs end?

Could be very, very relevant to read but I'm not sure what to make of it: Obtaining textbox value in change event handler

Here is the code that opens the form:

Private Sub Command7_Click()
    Dim strAHNS As String
    Dim strquery As String
    strAHNS = Replace(Mid(Me.Combo_Dessin2, InStr(Me.Combo_Dessin2, "=") + 1), "=", " ")
    strquery = "[ID] = (SELECT Max([ID]) FROM [Feuilles])"
    Debug.Print strquery
    If (PremierAffichage) Then
        DoCmd.OpenForm FormName:="f_feuillebleue", WhereCondition:=strquery
    Else
        MsgBox "Le projet ou dessin n'a pas été trouvé."
    End If
End Sub

Function PremierAffichage() As Boolean
    Dim rsFeuilles As DAO.Recordset
    Dim rsProjets As DAO.Recordset
    Dim strContrat As String
    Dim strProjet As String
    Dim strDessin As String
    Dim sqlquery As String
    Dim strAHNS As String
    Dim strGroupe As String
    Dim strMachine As String

    If IsNull(Me.Combo_Dessin2) Or IsNull(Me.Combo_Projet) Or Me.Combo_Dessin2.Value = "" Then
        PremierAffichage = False
        Exit Function
    End If

    strProjet = Me.Combo_Projet
    strAHNS = Me.Combo_Dessin2
    strMachine = Mid(strAHNS, 4, 3)
    strGroupe = Mid(strAHNS, 8, 2)
    Debug.Print strProjet & " ** " & strAHNS & " ** " & strMachine & " ** " & strGroupe

    sqlquery = "SELECT [AHNS], [Contrat], [No Projet], [EP (groupe)], [Type machine], [Mois] FROM [Feuilles]" 'WHERE [AHNS] = '" & strAHNS & "'"
    Set rsFeuilles = CurrentDb.OpenRecordset(sqlquery)

    sqlquery = "SELECT [Projet HNA] FROM [Projets] WHERE [Projet AHNS] = '" & strProjet & "'"
    Set rsProjets = CurrentDb.OpenRecordset(sqlquery)

    Debug.Print strAHNS    '========================================--------
    Form_f_FeuilleBleue.txt_AHNS = strAHNS ' this works in .value but not showing the result
    DoEvents '                         any changes from there on don't update the value visually
    '                       ==========================================------
    If rsProjets.RecordCount > 0 Then
        rsFeuilles.AddNew
        rsFeuilles![Contrat] = rsProjets![Projet HNA]
        rsFeuilles![No Projet] = strProjet
        rsFeuilles![AHNS] = strAHNS
        rsFeuilles![Mois] = MonthName(Mid(Date, 6, 2))
        If strMachine Like "[A-Z][A-Z][A-Z]" Then
            rsFeuilles![Type machine] = strMachine
            rsFeuilles![EP (groupe)] = strGroupe
        End If
        rsFeuilles.Update

        PremierAffichage = True
    End If

    rsProjets.Close
    Set rsProjets = Nothing

    rsFeuilles.Close
    Set rsFeuilles = Nothing

End Function
Community
  • 1
  • 1
David G
  • 2,315
  • 1
  • 24
  • 39
  • Is this just in debug? Try DoEvents after the line where you set it. – MatthewD Aug 17 '15 at 15:02
  • This is both in the immediate window and the actual code. DoEvents did not change anything (I just wrote DoEvents by itself on the next line, is that what you wanted to try?) – David G Aug 17 '15 at 15:05
  • Yes. I have seen it solve some odd issues like this. Is Form_f_FeuilleBleue the name of your textbox? Can you set it by Form_f_FeuilleBleue.text ? – MatthewD Aug 17 '15 at 15:06
  • I have never needed to use it but the form does have a repaint method. form1.repaint. See if that does anything. – MatthewD Aug 17 '15 at 15:08
  • That's the name of the form. I typed `Form_f_FeuilleBleue.txt_AHNS.text = "ee"` And it said that the control doesn't have the focus. If I write the same but with .Value, there is no error. The value is properly set - but the form doesn't show it. .Repaint did not change anything. – David G Aug 17 '15 at 15:10
  • Where do you expect it to display? – MatthewD Aug 17 '15 at 15:12
  • "ee" In the textbox itself in the form, visible to the user. Note; I manually wrote inside the textbox and asked for the value. It did not write the correct value either. I made sure I was working with the correct textbox. – David G Aug 17 '15 at 15:13
  • (I'd go to chat but it is blocked at work) When I open the form directly, it works as expected. The problem is only when I open the form through another form. Added this to the question. – David G Aug 17 '15 at 15:16
  • is that form being opened vbmodel? – MatthewD Aug 17 '15 at 15:19
  • I don't know what that means. It is opened with .openform – David G Aug 17 '15 at 15:21
  • Is there a reason you opened it that way? It might very well be related to that. Can you open it Form_f_FeuilleBleue.Show vbModal. You can leave off the vbModal if needed. – MatthewD Aug 17 '15 at 15:55
  • I don't think it's related to how I open it. After testing I realised that when I modify a textbox's value from another form, the Update and Change event don't fire - so it doesn't update it visually at all. I still don't know why. – David G Aug 17 '15 at 16:03
  • maybe put a public sub in that form that accepts the textbox to update and the value to put in it. Call that sub from the other form. – MatthewD Aug 17 '15 at 16:07
  • I'm not sure how that would work. I ''fixed'' it by storing the information in a public variable and on the Open event of the other form, load the textboxes using the public variables that have been filled. Non-solution but it works I guess. – David G Aug 17 '15 at 16:12
  • 1
    Also you can change the Update or Change event to public and call it manually after you set the value. Form_f_FeuilleBleue.txt_AHNS_Change – MatthewD Aug 17 '15 at 16:13

1 Answers1

1

Assign your value to the active form instance (the open form) instead of to the form's class.

So assuming the open form's name is f_FeuilleBleue and you want to assign that value to the form's txt_AHNS control ...

'Form_f_FeuilleBleue.txt_AHNS = strAHNS
Forms!f_FeuilleBleue!txt_AHNS = strAHNS

Reference the form by its name as a member of the Forms collection.

HansUp
  • 95,961
  • 11
  • 77
  • 135
  • I didn't know this could be done. Is there any other situation where the opposite is preferred? – David G Aug 17 '15 at 17:26
  • I think you would use *Form_f_FeuilleBleue* when you actually want to work with the class itself. But I don't really use that so can't say anything more useful. – HansUp Aug 17 '15 at 17:30