Solved

Need vba coding to delete all lines shown in a form coding module.

Posted on 2011-09-06
6
398 Views
Last Modified: 2016-08-29
Please note the following VBA code where I attempt to delete any of sfSubForm's coding modules...  For some reason when I do a Call FillSubForm("Table1") twice it fails to delete the coding the second time around and get the following error per image:

Also please note the uploaded sample provided here:
https://filedb.experts-exchange.com/incoming/ee-stuff/8144-DBSample.zip

And also this is a continuation of question:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_27288674.html
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

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
          
          If x = 1 Then
          
            ' Forms(frm.Name)(ctrl.name).ongotfocus="[event Procedure]"
            Forms(frm.Name)(ctrl.Name).OnGotFocus = "[Event Procedure]"
          End If
          
          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

Open in new window

amb-error.png
0
Comment
Question by:stephenlecomptejr
  • 3
  • 3
6 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36490734
the forms property Allow Design Changes is set to "Design View Only"

try changing this line

DoCmd.OpenForm frmname

with

DoCmd.OpenForm frmname,acDesign
0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 36491181
I still get the same error.
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 36493060
try this codes

Sub DeletefromForms(frmname)

    Dim mod1 As Module, strType As String
    Dim varLine As Integer
    '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, acDesign
    If Forms(frmname).HasModule = True Then
        Set mod1 = Forms(frmname).Module
        varLine = mod1.CountOfLines
        If varLine > 0 Then
            mod1.DeleteLines 1, varLine
          End If
        Set mod1 = Nothing
    End If
   
End Sub

0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 36493416
It works good.

Thank you - especially since you must've had to do this late tonight.
I appreciate it!
0
 
LVL 1

Author Closing Comment

by:stephenlecomptejr
ID: 36493418
Capricorn1, is the best.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36494897
;-)
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

948 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

22 Experts available now in Live!

Get 1:1 Help Now