Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modify two sub routines in to one function

Status
Not open for further replies.

pdeman

Programmer
Feb 17, 2010
39
0
0
GB
Hello,

I have the following two sub routines (please see below).

I need them to be combined in one function.

The subUpdatePageLinks must run before the subUpdateConnStr.

Also at present when the code runs it selects the dap in the database window making the database window visible even if it was hidden before which is no good.

I need the database window to remain hidden at all times and the object selected needs to be the first table in the hidden window.

Sub subUpdatePageLinks()
'
'Update the links for each data access page.
'
Dim aoDAP As AccessObject
Dim strLocation As String
Dim intPosition As Integer

'Retrieve the new path/location of the database.
intPosition = InStrRev(CurrentDb.Name, "\")
strLocation = Left$(CurrentDb.Name, intPosition)

'For each page...
For Each aoDAP In Application.CurrentProject.AllDataAccessPages

'Select the page.
DoCmd.SelectObject acDataAccessPage, aoDAP.Name, True

'Assign the new path/location to the page.
aoDAP.FullName = strLocation & aoDAP.Name & ".htm"
Next aoDAP

MsgBox "Links Updated."
End Sub


Sub subUpdateConnStr()
'
'This subroutine will update the ConnectionString property
'in each data access page within the current database.
'
Dim aoDAP As AccessObject
Dim dapObject As DataAccessPage

'Go through each data access page.
For Each aoDAP In Application.CurrentProject.AllDataAccessPages

'Open the current page.
DoCmd.OpenDataAccessPage aoDAP.Name, acDataAccessPageDesign

'Assign the current page to the data access page object.
Set dapObject = DataAccessPages(aoDAP.Name)

'Update the ConnectionString property of the current Page.
'CurrentDB.Name returns the path and file name to the database,
'that is, C:\Test\Northwind.mdb
dapObject.MSODSC.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & CurrentDb.Name

'Close the current page, saving the changes.
DoCmd.Close acDataAccessPage, dapObject.Name, acSaveYes
Next aoDAP

'Inform the user when the routine is complete.
MsgBox "Connection Strings Updated."
End Sub
 
Err, any reason you can't just write a new subroutine that calls both the existing subroutines?
Code:
Private Sub CallTwoSubs()
   subUpdatePageLinks
   subUpdateConnStr
End Sub

Good programming practice is to make your subroutines as small and single-purpose as possible. Combining two existing subroutines into one big one is really a backwards step.
 
The only reason was the modifications to overcome the way the current code works. I thought these changes might make one new routine better than two modified ones either way works for me it’s the changes that are important.

As I explained:

At present when the code runs it selects the dap in the database window making the database window visible even if it was hidden before which is no good.

I need the database window to remain hidden at all times and the object selected needs to end up as the first table in the hidden window.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top