Introduction.
VBA Module Object has several interesting methods and properties. Last week we saw how to insert a Click Event Procedure in a Form Module with a Function. You can find this blog post here.
I don’t say that the frm.Module.CreateEventProc() method, which we have tried, was an easy approach for writing a one-line statement in a Form Module. But, trying something different is always exciting programming like exploring the unknown. After all, it is there as part of the Application to explore and learn.
Today we will try an alternative and simple method for the same example we tried last week. That is to write all the program lines in a text file and load that program directly into the Form Module.
If you have tried last week’s example, we can use the same ‘Sample’ Form for today’s trial run, or do the following to get prepared:
Loading VBA Code from Text File.
Open a new Form in Design View.
Create a Command Button on the Detail Section of the Form.
While the Command Button is in the selected state display its Property Sheet (F4 or ALT+Enter).
Change the Name Property Value to cmdRun and the Caption Property Value to Run Report.
Save the Form with the name Sample.
If you have last week’s Sample form, then open it in Design View.
Display the Form Module, remove the existing program lines, and save the Form.
Open Notepad, copy and paste the following program lines into Notepad and save it as c:\windows\temp\vbaprg.txt:
Private Sub cmdRun_Click() DoCmd.OpenReport "myReport", acViewPreview End Sub
Replace the report name "myReport" with one of your own Report Names from the database.
Open a Standard VBA Module, copy and paste the following main program into the Standard Module:
The LoadFromTextFile() Function.
Public Function LoadFromTextFile() Dim frm As Form, frmName As String, ctrlName As String frmName = "Sample" 'ctrlName = "cmdRun" 'Open the form in design view DoCmd.OpenForm frmName, acDesign 'define the form object Set frm = Forms(frmName) 'call the form's Module Object's AddFromFile() method 'to read the program from the text file 'and insert them into the Form Module frm.Module.AddFromFile "c:\windows\temp\vbaprg.txt" 'Save and close the form with the code DoCmd.Close acForm, frmName, acSaveYes 'Open the form in Normal view DoCmd.OpenForm frmName, acNormal End Function
Place the cursor in the middle of the Code and press F5 to run the Code.
Press ALT+F11 to display the Database window with the Sample Form open.
Click on the Command Button to open the Report in print preview.
Close the Report.
Change the Sample Form in Design View.
- Open the form module and check for the program lines we have loaded from the vbaprg.txt file.
No comments:
Post a Comment
Comments subject to moderation before publishing.