code to build database

Lino

Active Member
Joined
Feb 27, 2002
Messages
429
Hello,

Does anyone have the code to build a database:

I want to take my data in excel and move it to an access database using vba.

thanks,

Lino
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
thank you.

how about this one:

I have an excel spreadsheet with columns A through I and then columns L, O, R and Z that i want to export to an access database.

I am guessing I need to set variables equal to these cells and assign each variable to a field in the access database to get this to work.

I could not find this on mvps.org...help.

thanks,

Lino
 
Upvote 0
This is the clip from that last link:
This would import from Excel into Access.

Code:
    DoCmd.TransferSpreadsheet transfertype:=acImport, _
            tablename:="tmpTableName", _
            FileName:="SomeExcelFile", Hasfieldnames:=True, _
            Range:="'WorkSheet Name'!", SpreadsheetType:=5
            
            'The Spreadsheet type = 5 specifies an Excel 5.0/7.0 file
            'format

This uses the built in Import function - same thing as if you went to the File-Get External Data-Import menu. The beauty of the method is, no, you do not need to pass fields into variables. Most of the time, Access will figure it out for you. It can even handle 'ignoring' columns in the middle that you don't want.

This is actually a function I wrote to handle my file import/exports/links

Code:
Public Function ImportExport(ByVal Ltype As String, ByVal Tname As String, _
                                 ByVal TLoc As String) As Long

On Error GoTo Err_handler
Debug.Print Ltype & " File " & TLoc
Select Case Ltype:
    Case "acImport":
      Ltype = 0
    Case "acExport":
      Ltype = 1
    Case "acLink":
      Ltype = 2
End Select

DoCmd.TransferSpreadsheet Ltype, 8, Tname, TLoc, True, ""
Exit Function

Err_handler:

 Select Case Err.Number:
  Case 2391:
    MsgBox Err.Number & " One or more fields in " & TLoc & " not in Database"
   Case 3051:
    ' Somebody is in the xls
  Case Else
    Debug.Print Err.Number & " " & Err.Description
 End Select
ImportExport = Err.Number
Err.Clear

End Function

Mike
 
Upvote 0
I would appreciate it if you could explain your code.

I don't see how u are bringing in the data?

Thanks,

Lino
 
Upvote 0
Lino said:
I would appreciate it if you could explain your code.

I don't see how u are bringing in the data?

Thanks,

Lino

The

Docmd.TransferSpreadsheet, acImport

is the command that transfers the data to your database
The acImport portion is stored in the variable Ltype, which is assigned based on the select case structure. The transferSpreadsheet can do a few different things, and his code allows for a passed variable that assigns the type of TransferSpreadsheet to be done.
 
Upvote 0

Forum statistics

Threads
1,221,674
Messages
6,161,215
Members
451,691
Latest member
fjaimes042510

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top