Solved

After dynamically creating form based on query/table - the _GotFocus VBA coding won't fire?

Posted on 2011-09-02
8
391 Views
Last Modified: 2016-08-29
Per the following link:  http://msdn.microsoft.com/en-us/library/aa139986(office.10).aspx under sections:  Programmatically Editing Modules - I'm trying to create a dynamic form based on a dynamic query and fill in that form a code module for the GotFocus event.  

The problem I'm having is that all the code in the GotFocus event won't run after the form opens.  

Please review the sample and tell me what syntax or event I need to add/change.

Here's the code sample I have in the database which is a continuation of question:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_27285631.html

Also here's a direct link to the sample:
https://filedb.experts-exchange.com/incoming/ee-stuff/8142-DBSample.zip

Thanks to all who review the sample!
Option Compare Database
Option Explicit

Public Sub FillSubForm(strSource As String, Optional fDelFind As Boolean)

    Dim frm As Form, ctrl As Control, fld As Field, CtrlType As Long
    Dim i As Long, r As Recordset, db As Database
    Dim strFldLst As String
    Dim dbFld As DAO.Database
    Dim rsFld As DAO.Recordset
    Dim x As Integer
    
    Dim sFldName As String
    
    Call DeletefromForms("sfSubForm")
    
    
    Set dbFld = CurrentDb()
    Set rsFld = dbFld.OpenRecordset("tblFieldFind")
    If Not IsNull(strSource) Then
       ' Put in a filler form while we rework the other one
       
       ' Open the subform for design
       DoCmd.OpenForm "sfSubForm", acDesign, , , , acHidden

       Set frm = Forms("sfSubform")
       frm.RecordSource = strSource
       
       ' Delete all the old controls from the previous recordsource
       For i = frm.Controls.Count - 1 To 0 Step -1
          Application.DeleteControl frm.Name, frm.Controls(i).Name
       Next i
       

    x = 1
    DoCmd.SetWarnings False
    DoCmd.OpenQuery "qdelFieldFind"
    If fDelFind Then
        DoCmd.OpenQuery "qdelAdvFind"
    End If
    DoCmd.SetWarnings True
    With rsFld
       ' Use this to find out what our source fields are
       Set r = CurrentDb.OpenRecordset(strSource, DB_OPEN_DYNASET)
        strFldLst = ""
       ' Build the controls.  Yes/No fields turn into checkboxes, all else into textboxes
       For Each fld In r.Fields
          If fld.Type = dbBoolean Then
             CtrlType = acCheckBox
          Else: CtrlType = acTextBox
          End If
          Set ctrl = Application.CreateControl(frm.Name, CtrlType, acDetail, , fld.Name)
          ctrl.Name = fld.Name
          strFldLst = strFldLst & fld.Name & ";"
          .AddNew
          !fieldID = x
          !FieldName = fld.Name
          !FieldDesc = fld.Name
          !FieldTypeID = fld.Type
          .Update
          x = x + 1
       Next fld
    End With
       ' Close the subform and put it back in our main form
       DoCmd.Close acForm, "sfSubform", acSaveYes
       DoCmd.OpenForm "sfSubform", acFormDS
       ' Make all the fields size to best fit
       For i = 0 To r.Fields.Count - 1
           
          
           
          If Forms!sfSubForm.Form.Controls(i).Name <> "Name" And _
            Forms!sfSubForm.Form.Controls(i).Name <> "Cutsheets" Then _
              Forms!sfSubForm.Form.Controls(i).ColumnWidth = -2
              
          If i = 0 Then
          
            sFldName = Forms!sfSubForm.Form.Controls(i).Name
            
          End If
       Next i
    End If
   
    Create_VBA_Code (sFldName)
   
    Set rsFld = Nothing
    dbFld.Close
    Set dbFld = Nothing

End Sub

Private Sub Create_VBA_Code(sFieldName As String)

  DoCmd.Close acForm, "sfSubForm", acSaveYes
  DoCmd.OpenForm "sfSubForm"
  Call InsertIntoForms("sfSubForm", sFieldName)
  DoCmd.Close acForm, "sfSubForm", acSaveYes
  DoCmd.OpenForm "sfSubForm", acFormDS
  
  
End Sub


Private Sub InsertIntoForms(frmname, sField As String)

    Dim mod1 As Module, strType As String

    'If Form has module, set reference to it
    'and insert line into the module.
    'Free reference resource when done.
    If Forms(frmname).HasModule = True Then
        Set mod1 = Forms(frmname).Module
        
        strType = "End Sub"
        mod1.InsertLines 1, strType
        
        strType = vbTab & "DoEvents"
        mod1.InsertLines 1, strType
        
        strType = vbTab & "DoCmd.RunCommand acCmdCopy"
        mod1.InsertLines 1, strType
        
        strType = vbTab & sField & ".SelLength = Len([" & sField & "])"
        mod1.InsertLines 1, strType
        
        strType = vbTab & sField & ".SelStart = 0"
        mod1.InsertLines 1, strType
        
        strType = vbTab & sField & ".SetFocus"
        mod1.InsertLines 1, strType
        
        strType = "Private Sub " & sField & "_GotFocus()"
        mod1.InsertLines 1, strType
        
        strType = ""
        mod1.InsertLines 1, strType
        
        strType = "Option Explicit"
        mod1.InsertLines 1, strType

        strType = "Option Compare Database"
        mod1.InsertLines 1, strType
        
        Set mod1 = Nothing
    End If
    
    
End Sub

Private Sub InsertIntoForms2(frmname, sField As String)

    Dim mod1 As Module, strType As String

    'If Form has module, set reference to it
    'and insert line into the module.
    'Free reference resource when done.
    If Forms(frmname).HasModule = True Then
        Set mod1 = Forms(frmname).Module
        'strType = "Option Compare" & vbEnter & "Option Explicit" & Chr(34) & "Private Sub " & sField & "_GotFocus()" & Chr(34) & "  MsgBox(" & sField & ".Value)" & "End Sub"
        Debug.Print strType
        mod1.InsertLines 1, strType
        Set mod1 = Nothing
    End If
    
    
End Sub


Sub DeletefromForms(frmname)

    Dim mod1 As Module, strType As String

    'If form has module, then check contents of first line
    'for "Module", and delete the first line if it is present.
    'Free module reference resource when done.
    DoCmd.OpenForm frmname
    If Forms(frmname).HasModule = True Then
        Set mod1 = Forms(frmname).Module
        If mod1.Find("Module", 1, 1, 1, 40) = True Then
            mod1.DeleteLines 1, 1
        End If
        Set mod1 = Nothing
    End If
    
End Sub

Open in new window

0
Comment
Question by:stephenlecomptejr
  • 4
  • 4
8 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
i did not see you add an event for the control, in the property sheet

something lke this
 
                Forms(sForm)(ctl.Name).ongotfocus = "[Event Procedure]"
0
 
LVL 1

Author Comment

by:stephenlecomptejr
Comment Utility
It makes sense of what you are telling me, capricorn1 but I'm not sure what syntax do I use to add it?  and where in the above subs?
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
try doing it after you created the control

      Set ctrl = Application.CreateControl(frm.Name, CtrlType, acDetail, , fld.Name)
           
            ctrl.Name = fld.Name
            if ctrl.name="whatever"
                      ctrl.Name.ongotfocus = "[Event Procedure]"
            end if
0
 
LVL 1

Author Comment

by:stephenlecomptejr
Comment Utility
capricron1,

This is the error I'm getting when using the code.
Please note all I'm trying to do is make the event procedure for the first field that comes up.

Thus if x = 1 Then....

errror-applying.png
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility
try including the reference to the form
 
     Forms(frm.Name)(ctrl.name).ongotfocus="[event Procedure]"

or

      Forms(frm.Name)(ctrl).ongotfocus="[event Procedure]"
0
 
LVL 1

Author Closing Comment

by:stephenlecomptejr
Comment Utility
Thank you always for your replies.

I'm sorry I forgot about the post - was thinking about the extra day off to get away from computers for awhile!
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
np.. btw, where are you going to use this dynamic form creation ?
0
 
LVL 1

Author Comment

by:stephenlecomptejr
Comment Utility
Where?  In a normal access database opened by users that resides as a front-end on their machines.  
I hope I answered your question as regards that...not sure why the question.

The coding above does work for the first got focus item but as you move down - the process does not throw the other items in the clipboard.  So I'm going to post another question as regards that problem.

Thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now