The previous post explained how to:
- create a Command Button for the Customer Form
- name the button
- create an On Click event
- open the Visual Basic Editor
Let us refresh for a moment where you are in the creation process. If you do not understand the terminology or exactly what you are doing, no matter. Understanding is secondary. Following these instructions produces results without understanding.
- You copied and pasted fncCustomerTable into your database. This function connected your database to QuickBooks® using QODBC and then created a table from the results.
- You tested the function by typing fncCustomerTable into the Immediate Window of the Visual Basic Editor.
- When you created the customer form, you chose the customer table as its record source. Remember the New Form Wizard where you chose a table? If not, that is fine.
- Instead of typing fncCustomerTable into the Immediate Window every time you need to refresh the customer table, you will now program a button to click. The button will use fncCustomerTable to reload customers into the database for you.
Overwriting a Form's Record Source
The function you created overwrites the table: tblCustomer_reloadable. You used a suffix (_reloadable) as a descriptive code. Coding tables, queries and functions provides clues for working on your database in the future. A suffix like _reloadable lets you know you may safely delete the table because your database contains code to re-create it.
Let us examine what happens when you try to overwrite a form's record source.
Between the lines Private Sub and End Sub, type:
Close the Visual Basic Editor and select Form View from the View Menu of the Microsoft® Access menubar. Save the form if you have not already done so.
Test the Button
Click the Command Button and wait while Microsoft® Access runs the function and displays the error message below.
Whenever you see this message, you know that Microsoft® Access tried to overwrite a table that is currently a record source for an open form, report or control. Microsoft® Access will not overwrite a table in use. We will fix this.
Fix the Button
You could be lazy and fix this by:
- clearing the form's record source as on line 100
- running the function as on line 110
- resetting the form's record source as on line 120.
Private Sub cmdReloadCustomers_Click()
100 Form.RecordSource = ""
120 Form.RecordSource = "tblCustomer_reloadable"
However, in the future you may create forms that use complicated queries or SQL statements for record sources. You may also need to recreate the form's record source using criteria that change by user selection. Do not let this confuse you, just continue and we will fix it properly.
A Better Fix
The better method is to:
- write a Function that creates the record source
- clear the form's record source
- perform the activity
- call the Function to re-create the form's record source
To accomplish this, delete fncCustomerTable from the code above and replace it with the code below. Be sure to keep the beginning of the procedure and the end of the procedure (Private Sub and End Sub)
'make the mouse pointer an hourglass100 Screen.MousePointer = 11
'clear the form's record source
110 Form.RecordSource = ""
'run the function that re-creates the customer table
'call the function that re-creates the form's record source
'make the mouse pointer an arrow
160 Screen.MousePointer = 0
Your Visual Basic Editor should now look like this:
Notice line 150 says: fncForm_RS. This is where Visual Basic leaves the button-click procedure to re-create the form's record source. We will write this function next. It is shorter than the one you just wrote so you are only minutes away from importing customers with a click.
Continue to the next post.
Do you like this post? Please link back to this article by copying one of the codes below.URL: HTML link code: BB (forum) link code: