Csv mdb Conversion Program

Source: Internet
Author: User

'/////////////////////////////////////// ////////////////
'Csv <-> MDB convert Tool
'Written by griefforyou
'/////////////////////////////////////// ////////////////
Option explicit

Private sub commandementclick ()
On Error goto errhandler
Commondialog1.filename = ""
Commondialog1.cancelerror = true
Commondialog1.filter = "CSV file (*. CSV; *. txt) | *. CSV; *. txt"
Commondialog1.showopen
If commondialog1.filename <> "" then
Text1.text = commondialog1.filename
End if
Exit sub

Errhandler:
Msgbox "error:" & err. Description, vbcritical, "error"
End sub

Private sub command2_click ()
On Error goto errhandler
Commondialog1.filename = ""
Commondialog1.cancelerror = true
Commondialog1.filter = "Access File (*. mdb) | *. mdb"
Commondialog1.showopen
If commondialog1.filename <> "" then
Text2.text = commondialog1.filename
End if
Exit sub

Errhandler:
Msgbox "error:" & err. Description, vbcritical, "error"
End sub

Private sub command3_click ()
If option1.value = true then
If Dir (text1.text) = "" then
Msgbox "CSV file does not exist! ", Vbcritical," error"
Exit sub
End if

If csv2mdb (text1.text, text2.text) = true then
Msgbox "Table imported successfully! ", Vbinformation," prompt"
End if
Else
If Dir (text2.text) = "" then
Msgbox "CSV file does not exist! ", Vbcritical," error"
Exit sub
End if

If mdb2csv (text2.text, text1.text, "book1") then
Msgbox "CSV exported successfully! ", Vbinformation," prompt"
End if
End if
End sub

Private function csv2mdb (csvfilename as string, mdbfilename as string, optional tablename as string = "") as Boolean
On Error goto errhandler
Dim strtemp as string
Dim strcsvfile as string, strcsvlinesplit as string
Dim icsvlinecount as integer, icsvfieldcount as integer
Dim strarrcsvline () as string, strarrcsvhead () as string, strarrcsvdata () as string

Dim I as integer, J as integer, RET as long

Dim adoxcat as ADOX. catalog, adoxtable as ADOX. Table
Dim adoconn as ADODB. Connection, adors as ADODB. recordset
Dim strcn as string

Dim filenum as integer

Csv2mdb = false

Filenum = freefile

Open csvfilename for input as filenum
While not EOF (filenum)
Strtemp = ""
Line input # filenum, strtemp
If trim (strtemp) <> "" and trim (strtemp) <> vbcrlf then
If strcsvfile = "" then
Strcsvfile = strtemp
Else
Strcsvfile = strcsvfile & vbcrlf & strtemp
End if
End if
Wend
Close filenum

If Len (strcsvfile) = 0 then
Msgbox "the CSV file is blank! ", Vbcritical," error"
Exit Function
End if

If instr (strcsvfile, vbcrlf)> 0 then
Strcsvlinesp133 = vbcrlf
Elseif instr (strcsvfile, vblf)> 0 then
Strcsvlinesp.pdf = vblf
Else
Msgbox "error CSV file! ", Vbcritical," error"
Exit Function
End if

Strarrcsvline = Split (strcsvfile, strcsvlinesp.pdf)
Icsvlinecount = ubound (strarrcsvline)

Strarrcsvhead = Split (strarrcsvline (0 ),",")
Icsvfieldcount = ubound (strarrcsvhead)

Strcn = "provider = Microsoft. Jet. oledb.4.0; Data Source =" & mdbfilename

Set adoxcat = new ADOX. Catalog
If Dir (mdbfilename) = "" then
Adoxcat. Create strcn
End if

If tablename = "" then
Tablename = getfilename (csvfilename)
End if

Adoxcat. activeconnection = strcn
For I = 0 to adoxcat. Tables. Count-1
If adoxcat. Tables (I). Name = tablename then
Ret = msgbox ("the table name already exists. Do you want to replace it? ", Vbokcancel + vbquestion," prompt ")
If ret = vbok then
Adoxcat. Tables. Delete tablename
Exit
Else
Set adoxcat = nothing
Exit Function
End if
End if
Next

Set adoxtable = new ADOX. Table
Adoxtable. parentcatalog = adoxcat
Adoxtable. Name = tablename
For I = 0 to icsvfieldcount
Adoxtable. Columns. append strarrcsvhead (I), advarwchar, 250
Adoxtable. Columns (strarrcsvhead (I). properties ("nullable") = true
Next

Adoxcat. Tables. append adoxtable

Set adoconn = new ADODB. Connection
Set adors = new ADODB. recordset
Adoconn. connectionstring = strcn
Adoconn. Open
Adors. cursorlocation = aduseclient
Adors. Open tablename, adoconn, adopenkeyset, adlockpessimistic

Redim strarrcsvdata (icsvlinecount) as string
For I = 1 to ubound (strarrcsvdata)
Strarrcsvdata = Split (strarrcsvline (I ),",")
Adors. addnew
For J = 0 to icsvfieldcount
Adors. Fields (j) = strarrcsvdata (j)
Next
Adors. Update
Next

Adors. Close
Set adors = nothing
Adoconn. Close
Set adoconn = nothing

Csv2mdb = true
Exit Function
Errhandler:
Msgbox "error:" & err. Description, vbcritical, "error"
End Function

Private function mdb2csv (mdbfilename as string, csvfilename as string, tablename as string) as Boolean
On Error goto errhandler

Dim adoconn as new ADODB. Connection
Dim adors as new ADODB. recordset
Dim RET as long
Dim strcn as string, strcsvline as string
Dim I as integer, J as integer
Dim filenum as integer

Mdb2csv = false
If Dir (csvfilename) <> "" then
Ret = msgbox ("the CSV file already exists. Do you want to overwrite it? ", Vbokcancel + vbquestion," prompt ")
If ret = vbok then
Kill csvfilename
Else
Exit Function
End if
End if

Strcn = "provider = Microsoft. Jet. oledb.4.0; Data Source =" & mdbfilename
Adoconn. connectionstring = strcn
Adoconn. Open
Adors. Open tablename, adoconn, adopenkeyset, adlockoptimistic

If adors. EOF then
Adors. Close
Set adors = nothing
Adoconn. Close
Set adoconn = nothing
Exit Function
End if
Filenum = freefile

Open csvfilename for output as filenum
For I = 0 to adors. Fields. Count-1
If strcsvline = "" then
Strcsvline = adors. Fields (I). Name
Else
Strcsvline = strcsvline & "," & adors. Fields (I). Name
End if
Next
Print # filenum, strcsvline

While not adors. EOF
Strcsvline = ""
For I = 0 to adors. Fields. Count-1
If strcsvline = "" then
Strcsvline = adors. Fields (I)
Else
Strcsvline = strcsvline & "," & adors. Fields (I)
End if
Next
Print # filenum, strcsvline
Adors. movenext
Wend
Close filenum

Adors. Close
Set adors = nothing
Adoconn. Close
Set adoconn = nothing

Mdb2csv = true
Exit Function

Errhandler:
Msgbox "error:" & err. Description, vbcritical, "error"
End Function

Private function getfilename (filename as string) as string
Dim strtemp as string
Strtemp = mid (filename, limit Rev (filename, "/") + 1)
Getfilename = left (strtemp, Len (strtemp)-4)
End Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.