SunQuest
 
           Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today!
  #1  
Old July 16th, 2004, 01:26 AM
hunterboy hunterboy is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 1 hunterboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
read delimited text file and insert into access database

i need to read a delimited text file and display it in access database
this is my text file:

Type Date Time Source Category Event User Computer
Success Audit 7/14/2004 10:04:30 AM Security Logon/Logoff 538 SYSTEM L340C53
Success Audit 7/14/2004 10:04:30 AM Security Logon/Logoff 538 SYSTEM L340C53
Success Audit 7/14/2004 10:01:44 AM Security Logon/Logoff 538 SYSTEM L340C53
Success Audit 7/14/2004 10:01:34 AM Security Logon/Logoff 540 SYSTEM L340C53

May i know how to code it in visual basic 6.0 to display in the database table?

Reply With Quote
  #2  
Old July 16th, 2004, 05:13 PM
Doug G Doug G is offline
Grumpier Old Moderator
ASP Free God 11th Plane (10000 - 10499 posts)
 
Join Date: Sep 2003
Posts: 10,143 Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 18 h 33 m 48 sec
Reputation Power: 180
In general, you'll want to write vb code that will open the text file and read lines from the text file to a variable.

Next you'll probably use the vb Split() function to split your input line into space-delimited pieces.

One you have the string split up, you can build an insert sql string and use an ADO connection to send the insert to the database.

There are tutorials here, and at sites like www.mvps.org
__________________
======
Doug G
======
I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain

Reply With Quote
  #3  
Old September 13th, 2004, 01:39 AM
lionellfw lionellfw is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 lionellfw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
can i have the exact link to the tutorial?
thanks alot.

Reply With Quote
  #4  
Old September 15th, 2004, 04:18 AM
lionellfw lionellfw is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 lionellfw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
i have an overflow problem regarding the codes below..can anyone help me? is it due to my text file having more than 66000 records? thanks. anyway..the codes below are of the question asked above..can anyone have to see if its correct? thanks again!

Private Sub Command1_Click()
Dim dbPath As String
dbPath = "db1.mdb"
Dim oConn As ADODB.Connection
Set oConn = New ADODB.Connection
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
dbPath
ImportTextFile oConn, "Newlog", "C:\temp\Event Logger\wmpub\export1.txt", " ", vbCrLf
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub

Public Function ImportTextFile(cn As Object, _
ByVal tblName As String, FileFullPath As String, _
Optional FieldDelimiter As String = " ", _
Optional RecordDelimiter As String = vbCrLf) As Boolean
'vbCrLf
'PARAMTERS: cn -- an open ado connection
' : tblName -- import destination table name
' : FileFullPath -- Full Path of File to import from
' : FieldDelimiter -- (Optional) String character(s) in
' file separating field values
' within a record; defaults
' to ","
' : RecordDelimiter -- (Optional) String character(s)
' separating records within text
' file; defaults to vbcrlf
'RETURNS: True if successful, false otherwise

Dim cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Dim sFileContents As String
Dim iFileNum As Integer
Dim sTableSplit() As String
Dim sRecordSplit() As String
Dim lCtr As Integer
Dim iCtr As Integer
Dim iFieldCtr As Integer
Dim lRecordCount As Long
Dim iFieldsToImport As Integer

'These variables prevent
'having to requery a recordset
'for each record
Dim asFieldNames() As String
Dim abFieldIsString() As Boolean
Dim iFieldCount As Integer
Dim sSql As String
Dim bQuote As Boolean
'On Error GoTo errHandler
'If Not TypeOf cn Is ADODB.Connection Then Exit Function
'If Dir("C:\temp\Event Logger\wmpub\export1.txt") = "" Then Exit Function

If cn.State = 0 Then cn.Open
Set cmd.ActiveConnection = cn
cmd.CommandText = tblName
cmd.CommandType = adCmdTable
Set rs = cmd.Execute
iFieldCount = rs.Fields.Count
rs.Close

ReDim asFieldNames(iFieldCount - 1) As String
ReDim abFieldIsString(iFieldCount - 1) As Boolean
For iCtr = 0 To iFieldCount - 1
asFieldNames(iCtr) = "[" & rs.Fields(iCtr).Name & "]"
abFieldIsString(iCtr) = FieldIsString(rs.Fields(iCtr))
Next

iFileNum = FreeFile
Open "C:\temp\Event Logger\wmpub\export1.txt" For Input As #iFileNum
sFileContents = Input(LOF(iFileNum), #iFileNum)
Close #iFileNum
'split file contents into rows
sTableSplit = Split(sFileContents, RecordDelimiter)
lRecordCount = UBound(sTableSplit)
'make it "all or nothing: whole text
'file or none of it
cn.BeginTrans
MsgBox lRecordCount
For lCtr = 0 To lRecordCount - 1
'split record into field values

sRecordSplit = Split(sTableSplit(lCtr), FieldDelimiter)
iFieldsToImport = IIf(UBound(sRecordSplit) + 1 < iFieldCount, UBound(sRecordSplit) + 1, iFieldCount)
'construct sql
sSql = "INSERT INTO " & Newlog & " ("

For iCtr = 0 To iFieldsToImport - 1
bQuote = abFieldIsString(iCtr)
sSql = sSql & asFieldNames(iCtr)
If iCtr < iFieldsToImport - 1 Then sSql = sSql & ","
Next iCtr

sSql = sSql & ") VALUES ("

For iCtr = 0 To iFieldsToImport - 1
If abFieldIsString(iCtr) Then
sSql = sSql & prepStringForSQL(sRecordSplit(iCtr))
Else
sSql = sSql & sRecordSplit(iCtr)
End If

If iCtr < iFieldsToImport - 1 Then sSql = sSql & ","
Next iCtr

sSql = sSql & ")"
cn.Execute sSql
Next lCtr
cn.CommitTrans
'rs.Close
Close #iFileNum
Set rs = Nothing
Set cmd = Nothing
ImportTextFile = True
Exit Function
'errHandler:
'On Error Resume Next
'If cn.State <> 0 Then cn.RollbackTrans
'If iFileNum > 0 Then Close #iFileNum
'If rs.State <> 0 Then rs.Close
'Set rs = Nothing
'Set cmd = Nothing
rs.Close
End Function

Private Function FieldIsString(FieldObject As ADODB.Field) As Boolean

Select Case FieldObject.Type
Case adBSTR, adChar, adVarChar, adWChar, adVarWChar, _
adLongVarChar, adLongVarWChar
FieldIsString = True
Case Else
FieldIsString = False
End Select

End Function

Private Function prepStringForSQL(ByVal sValue As String) _
As String
Dim sAns As String
sAns = Replace(sValue, Chr(39), "''")
sAns = "'" & sAns & "'"
prepStringForSQL = sAns
End Function

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > read delimited text file and insert into access database


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway