Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a macro in excel to save data in access database,excel(named DMI-Form-505-ToExcel.xlsm) and it easily save in access datbase(e.g. C:/Me/Project/DMI-Form-505-ToExcel.accdb). However, I want to give this to someone else and it will not work on their computer as they will have different path which is save in there system etc. How can I get the macro to work in other system (global path)?


this is my code:-
VB
Sub Mail_workbook_Outlook_2()
Dim newcon As ADODB.Connection
Set newcon = New ADODB.Connection
Dim Recordset As ADODB.Recordset

Set Recordset = New ADODB.Recordset
newcon.Open "provider=microsoft.ace.oledb.12.0;Data source= C:\Users\Reva 006\Desktop\New folder (3)\DMI-Form-505-ToAccess.accdb"
Recordset.Open "Sheet1", newcon, adOpenDynamic, adLockOptimistic


Recordset.AddNew

Recordset.Fields(0).Value = Range("c9").Value 'name
Recordset.Fields(1).Value = Range("C10").Value 'department
Recordset.Fields(2).Value = Range("C11").Value 'reason of request
Recordset.Fields(3).Value = Range("I9").Value 'ext
Recordset.Fields(4).Value = Range("I10").Value 'date
Recordset.Fields(5).Value = Range("C15").Value 'status
Recordset.Fields(6).Value = Range("A15").Value 'rev
Recordset.Fields(7).Value = Range("B15").Value ' document
Recordset.Fields(8).Value = Range("I15").Value 'Additional information
Recordset.Fields(9).Value = Range("F15").Value 'project
Recordset.Fields(10).Value = Range("G15").Value 'EWo
Recordset.Fields(11).Value = Range("H15").Value 'Autocad


Recordset.Update

Recordset.Close

End Sub
Posted
Updated 31-Aug-15 23:42pm
v3

1 solution

If i understand you well...

All what you need to do is to pass path to ms Access database as an argument:
VB
Sub Mail_workbook_Outlook_2(ByVal sDatabaseFile As String)
'
newcon.Open "provider=microsoft.ace.oledb.12.0;Data source= " & sDatabaseFile

'
End Sub


Usage:
VB
Sub Whatever
Dim vFileName As String
    
vFileName = Application.GetOpenFileName("Access database files (*.accdb), *.accdb")
If CStr(vFile) = CStr(False) Then Exit Sub

Mail_workbook_Outlook_2 CStr vFileName

End Sub


For further information, please see:
Sub Procedures[^]
Passing Arguments to Procedures[^]
Calling Sub and Function Procedures[^]
Application.GetOpenFilename Method (Excel)[^]
Programmatically Selecting Files in Excel for Windows and Excel for the Mac[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900