Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to pass a string aPath contains path of the active workbook to access vba module or sub. Below is my excel vba code that will open access db form. How do i pass the string value to access vba.

    Dim aPath, aDbase, aDSource, aTable, exePath As String
    Dim fileParam As String

    aPath = ActiveWorkbook.Path
    aDbase = "near_14.accdb"
    aDSource = aPath & "\" & aDbase

    Set appAccess = CreateObject("Access.Application")
    appAccess.Visible = True
    appAccess.OpenCurrentDatabase aDSource
    appAccess.DoCmd.OpenForm "Import_From_P"

    appAccess.CloseCurrentDatabase

My Access VBA code write contents back to excel. in which i want to have the aPath value in conWKB_NAME

   Public Sub sCopyResultstoexcel(conSHT_NAME As Variant, conWKB_NAME As Variant,  qrytable As String)
     'Copy records to first 20000 rows
     'in an existing Excel Workbook and worksheet

     Dim objXL As Excel.Application
     Dim objWkb As Excel.Workbook
     Dim objSht As Excel.Worksheet
     Dim db As Database
     Dim rs As Recordset
     Dim rs_Attribute As Recordset
     Dim intLastCol As Integer
     Const conMAX_ROWS = 20000
    Set db = CurrentDb
    Set objXL = New Excel.Application
    Set rs = db.OpenRecordset(qrytable, dbOpenSnapshot)

    With objXL
     .Visible = False
      Set objWkb = .Workbooks.Open(conWKB_NAME)
 On Error Resume Next
    Set objSht = objWkb.Worksheets(conSHT_NAME)
If Not Err.Number = 0 Then
  Set objSht = objWkb.Worksheets.Add
  objSht.Name = conSHT_NAME
End If
Err.Clear
On Error GoTo 0
intLastCol = objSht.UsedRange.Columns.Count
With objSht

    .Range(.Cells(2, 1), .Cells(conMAX_ROWS, _
        intLastCol)).CopyFromRecordset rs
        .Range(.Cells(1, 1), _
        .Cells(1, rs.Fields.Count)).Font.Bold = True
       '.Cells.Range(1, rs.Fields.Count).WrapText = True
       .Range(.Cells(1, 1), _
       .Cells(1, rs.Fields.Count)).WrapText = False
       'Formatting
With objSht.Range("A1:AP1")
.HorizontalAlignment = xlCenter
.ColumnWidth = "8"
.Font.Italic = False
.Font.Bold = True
.EntireColumn.ColumnWidth = 15
End With
      'Adding fields
    With rs
 For i = 1 To .Fields.Count
     objSht.Cells(1, i) = .Fields(i - 1).Name
 Next i
 objWkb.Save
End With
End With
 End With
 objWkb.Close
 objXL.Quit
        Set objSht = Nothing
        Set objWkb = Nothing
        Set objXL = Nothing
  Set rs = Nothing
  Set db = Nothing
  End Sub
share|improve this question
    
How will Access use that aPath string value? –  HansUp Mar 20 at 18:10
    
Sorry are you asking the question?.Yes,how do i make access uses the string value. –  vuyy1182 Mar 20 at 18:12
    
I meant what will Access do with aPath after it gets that string. –  HansUp Mar 20 at 18:13
    
i updated the question. please have a look –  vuyy1182 Mar 20 at 18:24

2 Answers 2

up vote 0 down vote accepted

Consider the Application.Run Method (see the Access help topic for details).

Try it like this ...

appAccess.OpenCurrentDatabase aDSource
appAccess.Run "sCopyResultstoexcel", "YourSheetName", aPath, _
    "your qrytable string"

I think that's what your question asks for. However that chain of operations (from Excel, open Access, and then from Access open Excel again) seems convoluted to me. I think it should be simpler to just pull the Access data into the target sheet in the workbook in the first Excel instance.

share|improve this answer
    
Yeah it looks like chain of operations. Briefly, User opens excel file and when click on button access form opens, there user will selects some data in dropdowns, corresponding to that i'll run query(method) to sortout the table. That query result table i need to write back to currently opened excel file. –  vuyy1182 Mar 20 at 18:54

Try this out. Dim aPath as String. Currently only exePath is declared as a String. Not sure it will work but worth a try. what error are you getting?

share|improve this answer
    
i'm not getting error. How do i pass aPath value to access vba sub. –  vuyy1182 Mar 20 at 17:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.