Version control Access 2007 database and application - database

I need to version control a Microsoft Access 2007 database and application. Currently everything is contained in a single mdb file.
The application includes:
Forms
VBA code
Actual database
I would assume I need to separate the database from the forms/code. I would like to be able to version control the forms/code as text to support version diffs.
At the moment I don't have access to SourceSafe (I heard there may be some access support) so I would prefer a solution that would work with subversion or git.

Access 2007 has a feature where you can split a DB into its Tables/Queries (backend) and Forms/Reports (front-end). Since your question mentions only version controlling the forms and modules, this might be a more elegant solution. I don't know where modules go after the split, so that might be a stumbling block.
Microsoft offers VSTO (Visual Studio Tools for Office), which will let you develop in VS and run version control via any VS plugin (CVS/SVN/VSS/etc.).
Finally, you can just directly connect to Visual Source Safe. This MSKB article has some good information and background to go through, while this Office Online article is designed for getting you up and running.
Ultimately, I would suggest against taking the code out of Access if at all possible. Assuming the VBA editor is your primary development environment, you'll be adding extra steps to your development process that cannot easily be automated. Every change you make will need to be manually exported, diff'd, and stored, and there is no Application.OnCompile event that you could use to export the changes. Even tougher, you'll have to manually import all changed source files from other developers when they do checkins.

I use the code below to extract the vba code from Excel files, you may be able to modify this to extract from Access.
Sub ExtractVBACode(strSource, objFSO, strExportPath, objLogFile)
Dim objExcel
Dim objWorkbook
Dim objVBComponent
Dim strFileSuffix
Dim strExportFolder
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = true
Set objWorkbook = objExcel.Workbooks.Open(Trim(strSource))
strExportFolder = strExportPath & objFSO.GetBaseName(objWorkbook.Name)
If Not objFSO.FolderExists(strExportFolder) Then
objFSO.CreateFolder(strExportFolder)
End If
For Each objVBComponent In objWorkbook.VBProject.VBComponents
Select Case objVBComponent.Type
Case vbext_ct_ClassModule, vbext_ct_Document
strFileSuffix = ".cls"
Case vbext_ct_MSForm
strFileSuffix = ".frm"
Case vbext_ct_StdModule
strFileSuffix = ".bas"
Case Else
strFileSuffix = ""
End Select
If strFileSuffix <> "" Then
On Error Resume Next
Err.Clear
objVBComponent.Export strExportFolder & "\" & objVBComponent.Name & strFileSuffix
If Err.Number <> 0 Then
objLogFile.WriteLine ("Failed to export " & strExportFolder & "\" & objVBComponent.Name & strFileSuffix)
Else
objLogFile.WriteLine ("Export Successful: " & strExportFolder & "\" & objVBComponent.Name & strFileSuffix)
End If
On Error Goto 0
End If
Next
objExcel.DisplayAlerts = False
objExcel.Quit
End Sub
Can you extract the forms as XML perhaps?

I've struggled with this same problem. I originally wrote code very much like the existing answer. The trick is to get all of your modules onto the file system, but that method has some drawbacks. Going that route, you can get your forms and reports out of the VBA Projects, but you can't get them back in. So, I created a library as part of our Rubberduck VBE Add-in. The library I wrote takes care of importing and exporting all of your code to/from the VBA project to/from the repository as you seemlessly push, pull, and commit. It's a free and open source project, so feel free to download and install the latest version.
Here is an example of how the library is used. I'll be adding actual integration with the VBA editor in a future release.
Dim factory As New Rubberduck.SourceControlClassFactory
Dim repo As Rubberduck.IRepository
Dim git As ISourceControlProvider
Dim xl As New Excel.Application
xl.Visible = true
Dim wb As Excel.Workbook
Set wb = xl.Workbooks.Open("C:\Path\to\workbook.xlsm")
' create class instances to work with
Set repo = factory.CreateRepository(wb.VBProject.Name, "C:\Path\to\local\repository\SourceControlTest", "https://github.com/ckuhn203/SourceControlTest.git")
Set git = factory.CreateGitProvider(wb.VBProject, repo, "userName", "passWord")
' Create new branch to modify.
git.CreateBranch "NewBranchName"
' It is automatically checked out.
Debug.Print "Current Branch: " & git.CurrentBranch
' add a new standard (.bas) code module and a comment to that file
wb.VBProject.VBComponents.Add(vbext_ct_StdModule).CodeModule.AddFromString "' Hello There"
' add any new files to tracking
Dim fileStat As Rubberduck.FileStatusEntry
For Each fileStat In git.Status
' fileStat.FileStatus is a bitwise enumeration, so we use bitwise AND to test for equality here
If fileStat.FileStatus And Rubberduck.FileStatus.Added Then
git.AddFile fileStat.FilePath
End If
Next
git.Commit "commit all modified files"
' Revert the last commit, throwing away the changes we just made.
git.Revert

Related

Open the latest file

I have an Access form with the drawing number D-A1ER-1378-1601-0 listed which is also stored in a file folder.
I use the code below to open the pdf drawing, which works fine.
Public Sub OpenDWG()
Dim strFile As String
Dim PathPDF As String
On Error GoTo Failure
PathPDF = DLookup("[FilePath]", "[SettingsDrawingFilePathTbl]", "ID = 4")
strFile = PathPDF & "\" & Screen.ActiveControl & ".pdf"
If Len(Dir(strFile)) Then
FollowHyperlink strFile
Else
MsgBox "No Document found for this Drawing Number, check Engineering Drawing Search File path in the Settings Tab and / drawing download files"
End If
Exit Sub
Failure:
MsgBox Err.Description
Err.Clear
End Sub
How do I adjust the strfile name
strFile = PathPDF & "\" & Screen.ActiveControl & ".pdf"
to get the form to open only the most recent file when a new version of the drawing is dropped into the folder. ie D-A1ER-1378-1601-0(2) will be the newest revision.
I would like to add a comment, but I don't have enough points ot comment.
I think that you can use the Dir function to get all files beginning with the same characters, or with wild cards, etc. I'll have to look into how exactly to do this. You could populate an array with these, and use code to scan the array to determine the latest file. Or you could populate a temporary table amd then use the table contents as the course to a combo box to have the user select the desired file, sorted with the latest one on top.
If I get a chance, I'll conjure up some sample code an post it.

How to get a value from nested / staggered Excel object - data from json

I have tried many JSON addins for Excel but I am having no luck parsing the JSON data below. finally I have managed to use the code below
Sub jsonDecode()
Dim jsonDecode As Variant
jsonText = Worksheets("Sheet3").Range("A1").Value
Set sc = CreateObject("ScriptControl"): sc.Language = "JScript"
Set jsonDecode = sc.Eval("(" + jsonText + ")")
End Sub
to create a staggered object but cannot access the values in the image below.
I have tried the following
msgbox(jsonDecode.location.id)
msgbox(tostring(jsonDecode.location.id))
msgbox(jsonDecode(location(id)))
Any help would be really appreciated for the code to get the values marked A and B in the image below :)
Forgive me if my terminology is a bit skewif
Cheers!!
Image of the array tree in Excel locals window
JSON text is
{"location":{"id":2456,"name":"Tuggerah","region":"Central Coast","state":"NSW","postcode":"2259","timeZone":"Australia/Sydney","lat":-33.30701,"lng":151.4159,"typeId":1},"forecasts":{"weather":{"days":[{"dateTime":"2016-09-13 00:00:00","entries":[{"dateTime":"2016-09-13 00:00:00","precisCode":"showers-rain","precis":"Late rain","precisOverlayCode":"","night":false,"min":10,"max":22}]}],"units":{"temperature":"c"},"issueDateTime":"2016-09-13 11:35:20"},"wind":{"days":[{"dateTime":"2016-09-13 00:00:00","entries":[{"dateTime":"2016-09-13 00:00:00","speed":9.1,"direction":287,"directionText":"WNW"},{"dateTime":"2016-09-13 01:00:00","speed":9.3,"direction":258,"directionText":"WSW"},{"dateTime":"2016-09-13 02:00:00","speed":9.3,"direction":256,"directionText":"WSW"},{"dateTime":"2016-09-13 03:00:00","speed":9.1,"direction":254,"directionText":"WSW"},{"dateTime":"2016-09-13 04:00:00","speed":6.9,"direction":260,"directionText":"W"},{"dateTime":"2016-09-13 05:00:00","speed":5.7,"direction":256,"directionText":"WSW"},{"dateTime":"2016-09-13 06:00:00","speed":5.7,"direction":249,"directionText":"WSW"},{"dateTime":"2016-09-13 07:00:00","speed":5.9,"direction":245,"directionText":"WSW"},{"dateTime":"2016-09-13 08:00:00","speed":5.2,"direction":254,"directionText":"WSW"},{"dateTime":"2016-09-13 09:00:00","speed":4.6,"direction":272,"directionText":"W"},{"dateTime":"2016-09-13 10:00:00","speed":4.6,"direction":281,"directionText":"W"},{"dateTime":"2016-09-13 11:00:00","speed":6.1,"direction":312,"directionText":"NW"},{"dateTime":"2016-09-13 12:00:00","speed":8,"direction":14,"directionText":"NNE"},{"dateTime":"2016-09-13 13:00:00","speed":9.6,"direction":45,"directionText":"NE"},{"dateTime":"2016-09-13 14:00:00","speed":9.8,"direction":56,"directionText":"NE"},{"dateTime":"2016-09-13 15:00:00","speed":9.6,"direction":77,"directionText":"ENE"},{"dateTime":"2016-09-13 16:00:00","speed":9.4,"direction":88,"directionText":"E"},{"dateTime":"2016-09-13 17:00:00","speed":10.7,"direction":73,"directionText":"ENE"},{"dateTime":"2016-09-13 18:00:00","speed":11.9,"direction":43,"directionText":"NE"},{"dateTime":"2016-09-13 19:00:00","speed":12.6,"direction":28,"directionText":"NNE"},{"dateTime":"2016-09-13 20:00:00","speed":11.7,"direction":11,"directionText":"N"},{"dateTime":"2016-09-13 21:00:00","speed":9.8,"direction":336,"directionText":"NNW"},{"dateTime":"2016-09-13 22:00:00","speed":7.8,"direction":318,"directionText":"NW"},{"dateTime":"2016-09-13 23:00:00","speed":4.6,"direction":304,"directionText":"NW"}]}],"units":{"speed":"km/h"},"issueDateTime":"2016-09-13 12:11:55"}},"forecastGraphs":{"temperature":{"dataConfig":{"series":{"config":{"id":"temperature","color":"#003355","lineWidth":2,"lineFill":false,"lineRenderer":"StraightLineRenderer","showPoints":false,"pointFormatter":"TemperaturePointFormatter"},"yAxisDataMin":10.7,"yAxisDataMax":22.2,"yAxisMin":0,"yAxisMax":32,"groups":[{"dateTime":1473724800,"points":[{"x":1473724800,"y":12.4},{"x":1473728400,"y":11.7},{"x":1473732000,"y":11.3},{"x":1473735600,"y":11},{"x":1473739200,"y":10.9},{"x":1473742800,"y":10.7},{"x":1473746400,"y":11},{"x":1473750000,"y":12.2},{"x":1473753600,"y":14.2},{"x":1473757200,"y":16.8},{"x":1473760800,"y":19.3},{"x":1473764400,"y":21},{"x":1473768000,"y":21.9},{"x":1473771600,"y":22.2},{"x":1473775200,"y":22.2},{"x":1473778800,"y":21.7},{"x":1473782400,"y":20.7},{"x":1473786000,"y":19.1},{"x":1473789600,"y":17.6},{"x":1473793200,"y":16.1},{"x":1473796800,"y":15.3},{"x":1473800400,"y":14.9},{"x":1473804000,"y":14.6},{"x":1473807600,"y":14.1}]}],"controlPoints":{"pre":{"x":1473721200,"y":10.2},"post":{"x":1473811200,"y":13.7}}},"xAxisMin":1473724800,"xAxisMax":1473811199},"units":{"temperature":"c"},"issueDateTime":"2016-09-13 07:21:53","nextIssueDateTime":"2016-09-13 08:21:53"},"precis":{"dataConfig":{"series":{"config":{"id":"precis","lineFill":false,"showPoints":true,"pointRenderer":"PrecisSummaryPointRenderer","pointFormatter":"PrecisSummaryPointFormatter"},"groups":[{"dateTime":1473724800,"points":[{"x":1473728400,"precisCode":"partly-cloudy","night":true},{"x":1473739200,"precisCode":"fog","night":true},{"x":1473750000,"precisCode":"mostly-cloudy","night":false},{"x":1473760800,"precisCode":"mostly-cloudy","night":false},{"x":1473771600,"precisCode":"mostly-cloudy","night":false},{"x":1473782400,"precisCode":"mostly-cloudy","night":false},{"x":1473793200,"precisCode":"chance-shower-cloud","night":true},{"x":1473804000,"precisCode":"showers-rain","night":true}]}],"controlPoints":[]},"xAxisMin":1473724800,"xAxisMax":1473811199}}},"observational":{"observations":{"temperature":{"temperature":18.5,"apparentTemperature":17.4,"trend":-1},"humidity":{"percentage":87},"dewPoint":{"temperature":16.3,"trend":1},"pressure":{"pressure":1019.3,"trend":null},"wind":{"speed":16.7,"gustSpeed":20.4,"trend":0,"direction":202.5,"directionText":"SSW"},"rainfall":{"lastHourAmount":0,"todayAmount":0,"since9AMAmount":0}},"stations":{"temperature":{"name":"Norah Head AWS","distance":15.5},"pressure":{"name":"Norah Head AWS","distance":15.5},"wind":{"name":"Norah Head AWS","distance":15.5},"rainfall":{"name":"Norah Head AWS","distance":15.5}},"issueDateTime":"2016-09-13 12:20:00","units":{"temperature":"c","amount":"mm","speed":"km/h","distance":"km","pressure":"hPa"}},"observationalGraphs":{"pressure":{"dataConfig":{"series":{"config":{"id":"pressure","color":"#003355","lineWidth":2,"lineFill":false,"lineRenderer":"StraightLineRenderer","showPoints":false,"pointFormatter":"PressurePointFormatter"},"yAxisDataMin":1018.2,"yAxisDataMax":1020.9,"yAxisMin":850,"yAxisMax":1100,"groups":[{"dateTime":1473724800,"points":[{"x":1473724800,"y":1019.4},{"x":1473728400,"y":1019.9},{"x":1473730200,"y":1019.4},{"x":1473732000,"y":1019.1},{"x":1473733800,"y":1018.7},{"x":1473735600,"y":1018.2},{"x":1473737400,"y":1018.5},{"x":1473739200,"y":1018.8},{"x":1473741000,"y":1019.1},{"x":1473742800,"y":1019.1},{"x":1473744600,"y":1019.3},{"x":1473746400,"y":1019.7},{"x":1473748200,"y":1020},{"x":1473750000,"y":1020.1},{"x":1473751800,"y":1020.5},{"x":1473753600,"y":1020.9},{"x":1473755400,"y":1020.9},{"x":1473757200,"y":1020.9},{"x":1473759000,"y":1020.4},{"x":1473760800,"y":1020.4},{"x":1473762600,"y":1020.5},{"x":1473764400,"y":1020.5},{"x":1473766200,"y":1019.8},{"x":1473768000,"y":1019.3}]}],"controlPoints":[]},"xAxisMin":1473724800,"xAxisMax":1473897599},"units":{"pressure":"hpa"},"provider":{"id":329,"name":"Norah Head AWS","lat":-33.28,"lng":151.58,"distance":15.5,"units":{"distance":"km"}}},"temperature":{"dataConfig":{"series":{"config":{"id":"temperature","color":"#003355","lineWidth":2,"lineFill":false,"lineRenderer":"StraightLineRenderer","showPoints":false,"pointFormatter":"TemperaturePointFormatter"},"yAxisDataMin":15.1,"yAxisDataMax":20.1,"yAxisMin":0,"yAxisMax":32,"groups":[{"dateTime":1473724800,"points":[{"x":1473724800,"y":15.6},{"x":1473725400,"y":16.1},{"x":1473726000,"y":16.1},{"x":1473726600,"y":16.1},{"x":1473727200,"y":15.8},{"x":1473727800,"y":15.9},{"x":1473728400,"y":16},{"x":1473729000,"y":15.9},{"x":1473729600,"y":15.9},{"x":1473730200,"y":15.8},{"x":1473730800,"y":15.6},{"x":1473731400,"y":15.4},{"x":1473732000,"y":15.4},{"x":1473732600,"y":15.4},{"x":1473733200,"y":15.5},{"x":1473733800,"y":15.3},{"x":1473734400,"y":15.3},{"x":1473735000,"y":15.1},{"x":1473735600,"y":15.3},{"x":1473736200,"y":15.3},{"x":1473736800,"y":15.5},{"x":1473737400,"y":15.5},{"x":1473738000,"y":15.5},{"x":1473738600,"y":15.4},{"x":1473739200,"y":15.5},{"x":1473739800,"y":15.6},{"x":1473740400,"y":15.7},{"x":1473741000,"y":15.8},{"x":1473741600,"y":15.9},{"x":1473742200,"y":16.1},{"x":1473742800,"y":16.2},{"x":1473743400,"y":16.4},{"x":1473744000,"y":16.4},{"x":1473744600,"y":16.4},{"x":1473745200,"y":16.4},{"x":1473745800,"y":16.3},{"x":1473746400,"y":16.3},{"x":1473747000,"y":16.4},{"x":1473747600,"y":16.4},{"x":1473748200,"y":16.5},{"x":1473748800,"y":16.6},{"x":1473749400,"y":16.8},{"x":1473750000,"y":16.8},{"x":1473750600,"y":16.9},{"x":1473751200,"y":17},{"x":1473751800,"y":17.1},{"x":1473752400,"y":17.4},{"x":1473753000,"y":17.4},{"x":1473753600,"y":17.6},{"x":1473754200,"y":17.9},{"x":1473754800,"y":17.9},{"x":1473755400,"y":17.9},{"x":1473756000,"y":17.9},{"x":1473756600,"y":18.2},{"x":1473757200,"y":18.2},{"x":1473757800,"y":18.3},{"x":1473758400,"y":18.2},{"x":1473759000,"y":18.3},{"x":1473759600,"y":18.4},{"x":1473760200,"y":18.6},{"x":1473760800,"y":18.8},{"x":1473761400,"y":18.7},{"x":1473762000,"y":18.6},{"x":1473762600,"y":18.4},{"x":1473763200,"y":18.6},{"x":1473763800,"y":19.2},{"x":1473764400,"y":20.1},{"x":1473765000,"y":19.6},{"x":1473765600,"y":20.1},{"x":1473766200,"y":20},{"x":1473766800,"y":20.1},{"x":1473767400,"y":19.2},{"x":1473768000,"y":18.8},{"x":1473768600,"y":18.6},{"x":1473769200,"y":18.5}]}],"controlPoints":[]},"xAxisMin":1473724800,"xAxisMax":1473897599},"units":{"temperature":"c"},"provider":{"id":329,"name":"Norah Head AWS","lat":-33.28,"lng":151.58,"distance":15.5,"units":{"distance":"km"}}}},"regionPrecis":{"days":[{"dateTime":"2016-09-13 00:00:00","entries":[{"dateTime":"2016-09-13 00:00:00","precis":"Cloudy. Patchy fog early this morning. High (70%) chance of rain in the late evening. Light winds becoming northeasterly 15 to 20 km/h in the late afternoon then tending northerly in the evening."}]}],"issueDateTime":"2016-09-13 10:41:16","name":"Central Coast"}}
Your approach uses JScript within VBA over ScriptControl object. This approach is not recommendable since ScriptControl object is only a 32-bit ActiveX component. It will not work with 64-bit versions of Office.
You can make it work if you accept that JScript objects are different from VBA objects. So you need a JScript method to get the JScript objects.
Example:
Sub jsonDecode()
Dim jsonDecode As Variant
jsonText = Worksheets("Sheet3").Range("A1").Value
Set sc = CreateObject("ScriptControl"): sc.Language = "JScript"
sc.AddCode "function getProperty(jsonObj, propertyName) { return jsonObj[propertyName]; } "
Set jsonDecode = sc.Eval("(" + jsonText + ")")
Set oLocation = sc.Run("getProperty", jsonDecode, "location")
MsgBox sc.Run("getProperty", oLocation, "id")
Set oForecasts = sc.Run("getProperty", jsonDecode, "forecasts")
Set oWeather = sc.Run("getProperty", oForecasts, "weather")
Set oDays = sc.Run("getProperty", oWeather, "days")
Set oDay0 = sc.Run("getProperty", oDays, "0")
MsgBox sc.Run("getProperty", oDay0, "dateTime")
End Sub
Here function getProperty is the JScript method to get the JScript objects.
But as stated already you should look for better methods parsing JSON with VBA. There are some if you search.
You may use the free Microsoft Excel add-In Power Query* (from Excel 2010) to browse and extract data from your JSON file.
On superuser you have an example.
*Power Query is known as Get & Transform in Excel 2016 and is full part of the software
The excel-requests Addin might be of help (disclaimer: I'm the author of this open source project).
You can find docs, installer script etc here: http://excel-requests.readthedocs.io/en/latest/.
Let me know if you need help.

Visual Basic Drag and Drop Window

I have found this code which allows me to drag and drop files onto to script icon and put them in a specified directory:
Const MyDestinationFolder = "C:\Temp\"
Const OverwriteExisting = True
Dim objFile,objFolder
Dim Arg
Set objFSO = CreateObject("Scripting.FileSystemObject")
If WScript.Arguments.Count > 0 Then
For Each Arg in Wscript.Arguments
Arg = Trim(Arg)
If InStr(Arg,".") Then
' Assume a File
Set objFile = objFSO.GetFile(Arg)
' Copy file to the Dest Folder using the same name
objFile.Copy MyDestinationFolder & objFile.Name,OverwriteExisting
Else
'Assume a Folder
Set objFolder = objFSO.GetFolder(Arg)
' Copy Folder to the Dest Folder
objFolder.Copy MyDestinationFolder, OverwriteExisting
End If
Next
End If
However I would like to make a script that runs and has a simple rectangle that says, drag and drop here. If this is at all possible, that would be great. Thanks!
You can add a GUI to VBScript programs by using "HTML Applications (HTAs)". Start your research here:
Introduction to HTML Applications (HTAs)
Extreme Makeover: Wrap Your Scripts Up in a GUI Interface
A Scriptomatic You Can Call Your Own
HTML Application
HTML Applications (HTAs)
Scripting Eye for the GUI Guy
and - of course
stackoverflow questions tagged hta
After second thoughts on "Drag & Drop", I found:
this claim and that .HTA (not tested)

Excel - VBA Question. Need to access data from all excel files in a directory without opening the files

So I have a "master" excel file that I need to populate with data from excel files in a directory. I just need to access each file and copy one line from the second sheet in each workbook and paste that into my master file without opening the excel files.
I'm not an expert at this but I can handle some intermediate macros. The most important thing I need is to be able to access each file one by one without opening them. I really need this so any help is appreciated! Thanks!
Edit...
So I've been trying to use the dir function to run through the directory with a loop, but I don't know how to move on from the first file. I saw this on a site, but for me the loop won't stop and it only accesses the first file in the directory.
Folder = "\\Drcs8570168\shasad\Test"
wbname = Dir(Folder & "\" & "*.xls")
Do While wbname <> ""
i = i + 1
ReDim Preserve wblist(1 To i)
wblist(i) = wbname
wbname = Dir(FolderName & "\" & "*.xls")
How does wbname move down the list of files?
You dont have to open the files (ADO may be an option, as is creating links with code, or using ExecuteExcel4Macro) but typically opening files with code is the most flexible and easiest approach.
Copy a range from a closed workbook (ADO)
ExecuteExcel4Macro
Links method
But why don't you want to open the files - is this really a hard constraint?
My code in Macro to loop through all sheets that are placed between two named sheets and copy their data to a consolidated file pulls all data from all sheets in each workbook in a folder together (by opening the files in the background).
It could easily be tailored to just row X of sheet 2 if you are happy with this process
I just want to point out: You don't strictly need VBA to get values from a closed workbook. You can use a formula such as:
='C:\MyPath\[MyBook.xls]Sheet1'!$A$3
You can implement this approach in VBA as well:
Dim rngDestinationCell As Range
Dim rngSourceCell As Range
Dim xlsPath As String
Dim xlsFilename As String
Dim sourceSheetName As String
Set rngDestinationCell = Cells(3,1) ' or Range("A3")
Set rngSourceCell = Cells(3,1)
xlsPath = "C:\MyPath"
xlsFilename = "MyBook.xls"
sourceSheetName = "Sheet1"
rngDestinationCell.Formula = "=" _
& "'" & xlsPath & "\[" & xlsFilename & "]" & sourceSheetName & "'!" _
& rngSourceCell.Address
The other answers present fine solutions as well, perhaps more elegant than this.
brettdj and paulsm4 answers are giving much information but I still wanted to add my 2 cents.
As iDevlop answered in this thread ( Copy data from another Workbook through VBA ), you can also use GetInfoFromClosedFile().
Some bits from my class-wrapper for Excel:
Dim wb As Excel.Workbook
Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
xlApp.DisplayAlerts = False ''# prevents dialog boxes
xlApp.ScreenUpdating = False ''# prevents showing up
xlApp.EnableEvents = False ''# prevents all internal events even being fired
''# start your "reading from the files"-loop here
Set wb = xlApp.Workbooks.Add(sFilename) '' better than open, because it can read from files that are in use
''# read the cells you need...
''# [....]
wb.Close SaveChanges:=False ''# clean up workbook
''# end your "reading from the files"-loop here
''# after your're done with all files, properly clean up:
xlApp.Quit
Set xlApp = Nothing
Good luck!
At the start of your macro add
Application.ScreenUpdating = false
then at the end
Application.ScreenUpdating = True
and you won't see any files open as the macro performs its function.

Any way to import multiple (csv) files to an Access db

I have multiple csv files with the same scheme, and I want to import them in one step. A solution could be to use the "import wizard", but I can only import one file with it. Oh, and it would be the best to work in msaccess2003. THX
The simplest solution is to start a dos-prompt, change to the directory where you have your files, and type:
type *.csv > allfiles.txt
If you do this often, you can create a batch-file that you can double-click from your desktop.
You can write a small program for importing see http://www.javaworld.com/javaworld/javaqa/2000-09/03-qa-0922-access.html for java JDBC conector to msaccess and since the import file is csv you can do this in no time...
There are other importing options for other languages
If all you want to do is drive the import with a list of files, you don't need a batch file. You can get the list of files using Dir():
Dim strCSVFileName As String
strCSVFileName = Dir("*.csv")
Do Until strCSVFileName = vbNullString
[import strCSVFileName]
strCSVFileName = Dir()
Loop
Of course, this assumes you're doing the import from within Access, but given your tags, that's the logical inference of your question.
This is an old thread, but it turned up when I searched for the issue. Hopefully this code helps someone address the same challenge. Builds / expands on the example David-W-Fenton offers, above.
I imported a file first, using the Wizard. Imported into a table named "bestTranscripts" and saved the import template as "BestImport" -- then used those values in the TransferText command.
Function ImportFiles()
On Error Resume Next
Dim cnn As New ADODB.Connection
Dim targetSet As New ADODB.Recordset
Dim sourceDirectoryName As String
Dim sourceFileName As String
sourceDirectoryName = "<path containing files>"
sourceFileName = Dir(sourceDirectoryName & "\*.txt")
Do Until sourceFileName = vbNullString
DoCmd.TransferText acImportDelim, "BestImport", "bestTranscripts", sourceFileName
sourceFileName = Dir()
Loop
End Function

Resources