'----------------------------------------------------------------------------------------
' File Name: frmExcelToMdb.frm
'
' Description: This file will convert an Excel Sheet to an MDB File
' Required Softwares :VB6, Excel, Access
'
'----------------------------------------------------------------------------------------
Private Sub CmdConvert_Click()
'Calling procedure to Create the Database
Call CreateAccessDatabase
Screen.MousePointer = vbHourglass
'Calling Procedure to Create table an dInsert the Values
Call CreateAndInsertIntoTable
Screen.MousePointer = vbNormal
End Sub
'----------------------------------------------------------------------------------------
' Function Name: CreateAccessDatabase()
'
' Description: This will Create an Access Database
'----------------------------------------------------------------------------------------
Sub CreateAccessDatabase()
On Error GoTo err1
Dim catNewDB As ADOX.Catalog 'Catalog Object
Set catNewDB = New ADOX.Catalog
'Creating Database
catNewDB.Create "Provider=Microsoft.Jet.OLEDB.3.51;" & _
"Data Source=" & App.Path & "\Test.MDB"
Set catNewDB = Nothing
Exit Sub
err1:
'If MDB Already exists then does nothing
If Err.Number = -2147217897 Then
Exit Sub
End If
End Sub
'----------------------------------------------------------------------------------------
' Function Name: CreateAndInsertIntoTable()
'
' Description: This will Create a table and will insert values to the Test Table
'----------------------------------------------------------------------------------------
Sub CreateAndInsertIntoTable()
Dim tbl As New Table
Dim cat As New ADOX.Catalog
Dim cn As New ADODB.Connection
Dim rec As New ADODB.Recordset
Dim fld As ADODB.Field
Dim recNew As New ADODB.Recordset
Dim strExcelPath As String
Dim intcnt As Long
'Opening Sheet in Excel Sheet Like a Table
rec.Open "Select * from [Sheet1$]", cn, adOpenKeyset
'To delete If "Test" Table is already there
If cat.Tables.Count <> 4 Then cat.Tables.Delete "Test"
'------------------------------------------------------------------------------------
'Creating Table
'------------------------------------------------------------------------------------
'Assigning Table Name
tbl.Name = "Test"
'Appending Column-The Col Name will be the Column name from the Sheet
For Each fld In rec.Fields
tbl.Columns.Append fld.Name, adChar, 200
Next
'Creating Table using Catalog Object of ADOX
cat.Tables.Append tbl
'Opening the Newly created table in Test.MDB
recNew.Open "Test", cat.ActiveConnection, adOpenKeyset, adLockOptimistic
'Making Pg Bar Visible
ProgressBar1.Visible = True
'Setting Max value of Pg BAr as No of Records in the Sheet
If rec.RecordCount <> 0 Then
ProgressBar1.Max = rec.RecordCount
End If
'Initialising Counter
intcnt = 1
Do Until rec.EOF
'Calling DoEvents so as to see the counter correctly
DoEvents
With recNew
.AddNew
For Each fld In rec.Fields
'Assigning value to the recNew Recordset for Insertion
.Fields(fld.Name) = IIf(IsNull(rec(fld.Name)), "", rec(fld.Name))
Next
.Update
End With
'Assigning Value to PgBar Control
ProgressBar1.Value = intcnt
'Assigning Value to Label Control
lblStatus.Caption = "Added " & intcnt & " Records..."
'Incrementing Counter
intcnt = intcnt + 1
'Moving to Next Record
rec.MoveNext
Loop
DoEvents
'Showing the Location of MDB File
lblStatus.Caption = "Open the MDB File at " & App.Path & "\Test.Mdb"
ProgressBar1.Visible = False
End Sub
Private Sub Form_Load()
Me.Height = 3015
End Sub