0% found this document useful (0 votes)
11 views2 pages

Mdbtosqlvbascript

The document provides a VBA script to export an Access database (.mdb) to a SQL file (.sql). It creates SQL commands for table creation and data insertion based on the database schema and data types. The script includes a helper function to map Access data types to SQL types and saves the output to a specified file path.

Uploaded by

Satvik Salat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Mdbtosqlvbascript

The document provides a VBA script to export an Access database (.mdb) to a SQL file (.sql). It creates SQL commands for table creation and data insertion based on the database schema and data types. The script includes a helper function to map Access data types to SQL types and saves the output to a specified file path.

Uploaded by

Satvik Salat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//.mdb to .

sql
in VBA alt+f11 to VBA then insert new module and save with mdbtosel :then write
below code:

Sub ExportMDBToSQL()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim rst As DAO.Recordset
Dim sqlFile As Integer
Dim exportPath As String
Dim tableName As String
Dim fieldList As String
Dim valueList As String
Dim sqlLine As String

' Change this to your desired export path


exportPath = "D:\BCA 2016thi\Marks.sql"

Set db = CurrentDb
sqlFile = FreeFile
Open exportPath For Output As #sqlFile

' Loop through all user tables


For Each tdf In db.TableDefs
If Left(tdf.Name, 4) <> "MSys" And Left(tdf.Name, 1) <> "~" Then
tableName = tdf.Name

' --- CREATE TABLE ---


sqlLine = "CREATE TABLE [" & tableName & "] (" & vbCrLf
For Each fld In tdf.Fields
sqlLine = sqlLine & " [" & fld.Name & "] " & GetSQLType(fld.Type)
& "," & vbCrLf
Next
sqlLine = Left(sqlLine, Len(sqlLine) - 3) & vbCrLf & ");" & vbCrLf &
vbCrLf
Print #sqlFile, sqlLine

' --- INSERT DATA ---


Set rst = db.OpenRecordset(tableName, dbOpenSnapshot)
Do While Not rst.EOF
fieldList = ""
valueList = ""
For Each fld In rst.Fields
fieldList = fieldList & "[" & fld.Name & "], "
If IsNull(fld.Value) Then
valueList = valueList & "NULL, "
ElseIf fld.Type = dbText Or fld.Type = dbMemo Then
valueList = valueList & "'" & Replace(fld.Value, "'", "''")
& "', "
Else
valueList = valueList & fld.Value & ", "
End If
Next
fieldList = Left(fieldList, Len(fieldList) - 2)
valueList = Left(valueList, Len(valueList) - 2)
Print #sqlFile, "INSERT INTO [" & tableName & "] (" & fieldList &
") VALUES (" & valueList & ");"
rst.MoveNext
Loop
rst.Close
Print #sqlFile, vbCrLf
End If
Next

Close #sqlFile
MsgBox "Export completed!" & vbCrLf & "Saved to: " & exportPath
End Sub

' Helper function to map Access data types to SQL types


Function GetSQLType(accessType As Integer) As String
Select Case accessType
Case dbBoolean: GetSQLType = "BIT"
Case dbByte: GetSQLType = "TINYINT"
Case dbInteger: GetSQLType = "SMALLINT"
Case dbLong: GetSQLType = "INTEGER"
Case dbSingle: GetSQLType = "REAL"
Case dbDouble: GetSQLType = "DOUBLE"
Case dbCurrency: GetSQLType = "MONEY"
Case dbDate: GetSQLType = "DATETIME"
Case dbText: GetSQLType = "VARCHAR(255)"
Case dbMemo: GetSQLType = "TEXT"
Case dbGUID: GetSQLType = "UNIQUEIDENTIFIER"
Case Else: GetSQLType = "VARCHAR(255)"
End Select
End Function

You might also like