|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Excel to MDB conversion in VB 6.0 facing some problems
Hello!
I am currently exporting my database from Excel (.xls) to single Microsoft Access (.mdb) file using this code. There are 24 excel files with single sheets (different sheet names in every file) and I will preprocess and put them into a single MDB file with 24 different tables. The excel files have similar data, for customer numbers but for different months, 24 months i.e. 2 years data. The columns in all excel files are equal, but the rows are not, meaning some customers are not present in every excel file. Just FYI: This is my masters project. My first preprocessing requires to extract all customers and their data into a separate (.mdb) file which have data for all 24 months, which will then be implemented into a intelligent statistical based system for checking good and bad customers based on some criteria. Q1: Customer numbers in every file are in the second column, and I remove them if there are any duplicate customers with Excel 2007. The data is too long (50,000+ rows) which cannot be done manually for every excel file.. i.e. for every excel file if customer numbers in the second column are same, the entire rows for those particular customer number are removed using Excel's removing duplicate columns function. I have seen this type of procedure can be done using Macros in VBA, but my project requires me to use VB, as the project I give to the company they will use other data files later on, which cannot be integrated into the current system. Can anyone give me advice from where to start off if this can be accomplished using VB? Q2: Secondly when I am reading Excel files the sheet name in every file (there is only 1 sheet in every excel file) has to be read through an automated procedure.. and currently I am using an array " $ mytype $ " in my pasted code to do this for every excel file. Is it possible that I can integrate reading every excel sheet name automatically and then export the database to MDB rather than specifying the sheet names in an array? How to integrate this it into my current ode? Using excel object? Again I am discouraged here to use VBA and Macros. Q3. Thirdly the major problem is when I delete the duplicate customer numbers in my excel files using Excel 2007, the rows deleted create empty rows additionally at the end of the excel files.. and for my case there are approximately 3000-4000 empty excel rows created for each excel file. My current code reads these EMPTY rows also in the end of the excel, which messes up my conversion into my MDB database. Is there a way I can skip reading empty rows at the end of the file which do not have any data? Ofcourse I can delete them manually using excel again, but it is time consuming and needs to be automated. Currently I think maybe using sql select, to do this.. will it be handy? Here is my code for excel to mdb conversion: Code:
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
Dim month As Variant
month = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24)
Dim lngPosition As Long
Dim mtype As Variant
mtype = Array("Jan2006", "Feb2006", "Mac2006", "Apr2006", "May2006", "Jun2006", "Jul2006", "Aug2006", "Sep2006", "Okt2006", "Nov2006", "Dec2006", "Jan2007", "Feb2007", "Mac2007", "Apr2007", "May2007", "Jun2007", "Jul2007", "Aug2007", "Sep2007", "Okt2007", "Nov2007", "Dec2007")
'Loop for all 24 months
For lngPosition = LBound(month) To UBound(month)
'Excel File Path
strExcelPath = App.Path & "\" & month(lngPosition) & ".xls"
'Opening Catalog Connection
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & App.Path & "\data.mdb"
'Opening Excel Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & strExcelPath & ";Extended Properties=Excel 8.0;"
'Opening Sheet in Excel Sheet Like a Table
rec.Open "Select * from [0121_" & mtype(lngPosition) & "$]", cn, adOpenKeyset
'------------------------------------------------------------------------------------
'Creating Table
'------------------------------------------------------------------------------------
'Assigning Table Name
Set tbl = New ADOX.Table
tbl.Name = mtype(lngPosition)
'Appending Column-The Col Name will be the Column name from the Sheet
For Each fld In rec.Fields
tbl.Columns.Append fld.Name
Next
'Creating Table using Catalog Object of ADOX
cat.Tables.Append tbl
'Opening the Newly created table in data.mdb
recNew.Open mtype(lngPosition), 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 & "\data.mdb"
ProgressBar1.Visible = False
cn.Close
recNew.Close
Next lngPosition
End Sub
|
|
#2
|
||||
|
||||
|
Q1. Have you considered importing it straight then running a query to find and delete duplicates?
Q2. If you use the import wizard by default the table name becomes the name of the sheet (you'll see table "Sheet1" a lot by nature if you use this stuff a lot) You can also easily get it by using VBA to open the workbook and get the sheet name of the first sheet. (Don't every forget to close quit and unload the excel object lest you end up with 50 instances of Excel in memory like ~someone~ I know has ... ) Yes using a For Each loop on the Sheets and reading the sheet name is an easy way to loop through all the sheets in a workbook. I do this often for importing. Q3. That would be not much a big deal if you handled your data clean up on the access side where it would be not only cleaner and easier but faster. If you needed to a delete query can easily kill empty records post import ... I strongly suggest trying DoCmd.TransferSpreadsheet to get the data into access then go about making it right with update queries and such. I rarely do it any other way.
__________________
medialint.com Now I don't know, but I been told it's hard to run with the weight of gold. Other hand I have heard it said, it's just as hard with the weight of lead. Last edited by medialint : April 19th, 2008 at 12:22 PM. |
|
#3
|
|||
|
|||
|
Thanks, I have found the solution to Q2. Its very simple:
Code:
Dim xlApp As Excel.Application
Dim xlWb As Excel.Workbook
Dim xlSName As String
'Get excel worksheet name
Set xlApp = New Excel.Application
Set xlWb = xlApp.Workbooks.Open(App.Path & "\" & month(lngPosition) & ".xls")
xlSName = xlWb.Worksheets(1).Name
Debug.Print xlSName
'Close excel
Set xlWb = Nothing
xlApp.Quit
Set xlApp = Nothing
However, excel.exe in my case will open and close 24 times, which will be more memory and time consuming. Thanks I will look into DoCmd.TransferSpreadsheet, was not aware of it previously. Currently I am thinking to use sql to integrate all things into a sql line and execute it: (1) Select entire table from excel worksheet (2) Remove the rows in the table corresponding to duplicates in the customer number column (3) Remove the blank lines in the end of the excel file Currently I have come up with this code for (1) and (3): Code:
'Do not copy empty rows at the end of the excel file
sqlstr = "Select * from [" & xlSName & "$]"
sqlstr = "DELETE FROM " & sqlstr & " WHERE CustomerNo IS NULL"
'Opening Sheet in Excel Sheet Like a Table
rec.Open " & sqlstr & ", cn, adOpenKeyset
For (2) I am not currently sure if it can be done with sql or it needs Macros in VBA... I am still brainstorming the use of sql select for this type of filtering.. Example of (2) how it should be like: Input table: Table_1 -------------------------------------------------------------------- StnCode CustomerNo Name1 Name2 0121 00200002 HARWANT REALTY SDN BHD (G.S. GILL) 0121 00200003 EUROPE KITCHEN STATION SDN BHD 168 0121 00200004 COLISEUM CAFE & HOTEL 0121 00200005 SKT V K KALYANASUNDRAM SDN BHD (KOWLOON) 0121 00200006 MAJLIS AMANAH RAKYAT (M.A.R.A) 0121 00200006 MAJLIS RAKYAT SDN 0121 00200007 F T LAND SDN BHD 0121 00200008 CYCLE & CARRIAGE BINTANG BHD. 0121 00200010 YAYASAN UBAIDI (MYDIN) 0121 00200011 MALAYAN BANKING BHD. Output: Table_2 -------------------------------------------------------------------- StnCode CustomerNo Name1 Name2 0121 00200002 HARWANT REALTY SDN BHD (G.S. GILL) 0121 00200003 EUROPE KITCHEN STATION SDN BHD 168 0121 00200004 COLISEUM CAFE & HOTEL 0121 00200005 SKT V K KALYANASUNDRAM SDN BHD (KOWLOON) 0121 00200007 F T LAND SDN BHD 0121 00200008 CYCLE & CARRIAGE BINTANG BHD. 0121 00200010 YAYASAN UBAIDI (MYDIN) 0121 00200011 MALAYAN BANKING BHD. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Excel to MDB conversion in VB 6.0 facing some problems |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|