Question

I am creating 2 reports in one sheet using excel macro. The reports connect to Oracle DB to pull the data. I have the connect string that pulls the data and it work fine. What I am trying to do is move the connection string to a function and pass it to the respective report based on user input. Here is a high level flow: 1. User clicks on a menu button. In the background it tries to connect to DB. 2. If the connection is successful it opens a form with 2 buttons (which report the user wants to run). Button A and Button B 3. Suppose User clicks Button A the report should be executed and displayed.

The issue I am facing is in step 2. For some reason I am not able to pass the connection parameters to Button A function.

Below is the snippet of the code on how I am trying to connect to the dB. need help on how to pass connection parameters to Button A function.

Code for connecting to DB

       Dim dbConnect As ADODB.Connection
       Dim GetData As ADODB.Recordset

       'Declare a set of variables to hold the username and password for the database
       Dim strUserName As String
       Dim strPassword As String
       Dim strDatabase As String
       Set GetData = New ADODB.Recordset
      '***

   On Error Resume Next


      Set dbConnect = New ADODB.Connection

      dbConnect.Open ( _
    "User ID=" & strUserName & "; Password=" & strPassword & "; Data Source=" & strDatabase & "; Provider=msdaora")

      'If the username or password is incorrect throw an error message
      If (dbConnect.State <> 1) Or (Err <> 0) Then
       intResult = MsgBox("Could not connect to the database.  Check your user name and password." & vbCrLf & Error(Err), 16, " ")
    Else
      '.ConnectionString = dbConnect
      ReportGenerator.Show ' this opens up another form where 
                           '  user can input the dates.
                           'This is turn calls the actual code...
      End If

      End Sub

In the Report generator form passing the date parameters to the function

Private Sub Login_Click()
    Call ReportGenerator(FromDate.Value, ToDate.Value)
End Sub

I need to pass the connection parameters to CallReportGenerator function.

Any pointers will be helpful.

Was it helpful?

Solution

There are multiple was you can do this:

Option 1: Use a global variable

If you declare a variable as Global on the top of a module, i.e. before any subs/functions, you can access them from any other module:

Example - Module1:

Option Explicit

Global gStrConnection As String 'g for Global scope

Sub Connect()
    gStrConnection = "User ID=" & strUserName & "; Password=" & strPassword & "; Data Source=" & strDatabase & "; Provider=msdaora")    
   dbConnect.Open gStrConnection
   ...
End Sub

Form:

Private Sub CreateReport()
    dbConnect.Open gStrConnection
    ...
End Sub

Option 2: Pass the variable to a local form variable

Declare a module wide variable in the Form's module (i.e. where you place the other code of the form). Then provide a small setter sub that assigns the value.

Example - Module1:

Option Explicit

Sub Connect()
    Dim strConnection as String
    strConnection = "User ID=" & strUserName & "; Password=" & strPassword & "; Data Source=" & strDatabase & "; Provider=msdaora")    
   dbConnect.Open strConnection
   ...
    ReportGenerator.StoreConnectionString strConnection
    ReportGenerator.Show
    ...
End Sub

Form:

Private mStrConnection as String 'm for module-wide scope

Public Sub StoreConnectionString(strConnection as String)
    mStrConnection = strconnection
End Sub

Private Sub CreateReport()
    dbConnect.Open mStrConnection
    ...
    ...
End Sub

OTHER TIPS

Declare the connection and the recordset at the top of the standard module, outside of any procedures and set their accessors to public.

Remember to clean up resources after you're done.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top