Jag har en modul som jag hittat (skriven av Pelle) som jag modifierat lite. Problemet är att jag inte får den att läsa sista raden i filen. Den läser alla rader och lägger in dom i vår Movex databas utom den sista, någon som ser felet? Ingen mening att skriva Dim om du inte talar om vad det skall bli. Tack Sven, Så här skulle jag vilja snygga till hela SubenLäsa sista raden i en textfil
Global Con As ADODB.Connection
Global rst As ADODB.Recordset
Enum dbDatabaseType
dbSQLServer = 1
dbOracleServer = 2
dbAccess = 3
End Enum
Enum dbRecordsetType
dbOpenAsTable = 1
dbOpenAsQuery = 2
End Enum
Public Function CreateRecordset(Indata As String, RecordsetType As dbRecordsetType) As ADODB.Recordset
' Input: Indata
' Här kan du antigen ange ett tabellnamn
' eller en select-sats
' Retur: CreateRecordset
' Det data som returneras
Select Case RecordsetType
Case 1
Dim rsTmp As New ADODB.Recordset
rsTmp.CursorLocation = adUseClient
rsTmp.Open Indata, Con, adOpenDynamic, adLockBatchOptimistic, adCmdTable
Case Else
Set rsTmp = Con.Execute(Indata)
End Select
Set CreateRecordset = rsTmp
Set rsTmp = Nothing
End Function
Public Function StartConnection(DatabaseType As dbDatabaseType) As Boolean
' Input: DataBaseType
' 1 = SQL Server
' 2 = Accessdatabas (default)
' Retur: StartConnection
' True = Ok
' False = Något fel
Select Case DatabaseType
Case 1: ConStr = "Provider=SQLOLEDB.1;Persist Security Info=False;" & _
"User ID=sa;Initial Catalog=Northwind;" & _
"Data Source=pellesoft"
Case Else: ConStr = "DSN=FORRAD;" & _
"Uid=bwn;" & _
"Pwd=amd64fx;"
End Select
On Local Error Resume Next
Set Con = CreateObject("adodb.connection")
Con.Open ConStr
If Err Then
StartConnection = False
Else
StartConnection = True
End If
End Function
Public Sub StopConnection()
Set rst = Nothing
Set Con = Nothing
End Sub
Public Sub CreateTable()
Dim Buffer
Dim vArray
' hämtar lite värden
FileName = Form1.txtFileName
TableName = Form1.txtTable
' öppnar filen
Open FileName For Input As #1
orgsql = "insert into " & TableName & " (BDATE,BTIME,BITNO,BFUT,BFIN,BAVD,BANSV,BMASK,BPROJ,BFMAN) values("
Do While Not EOF(1)
Line Input #1, Buffer
If EOF(1) Then Exit Do
vArray = Split(Buffer, "|")
insertsql = orgsql
For a = 0 To 9
' alla värden
insertsql = insertsql & "'" & vArray(a) & "',"
Next
' tar bort avslutande "
insertsql = Left(insertsql, Len(insertsql) - 2) & "')"
' adderar posten
Con.Execute insertsql, , adCmdText + adExecuteNoRecords ' = 129
Loop
Close
End Sub
Textfilen ser ut som följer:
07-10-03|14:18:29|910455 | 1| |331500 |1025|33-00-023 | |1069|
07-10-03|14:19:17|TS18252ST | 1| |331501 |1025|33-02-025 | |1069|
07-10-03|14:19:40|TS18252ST | 1| |331500 |1025|30-02-025 | |1069|Sv: Läsa sista raden i en textfil
Public Sub CreateTable()
Dim Buffer As String
Dim vArray() As String
'.....................
'....................
Do Until EOF(1)
Line Input #1, Buffer
vArray = Split(Buffer, "|")
insertsql = orgsql
For a = 0 To 9
' alla värden
insertsql = insertsql & "'" & vArray(a) & "',"
Next
' tar bort avslutande "
insertsql = Left(insertsql, Len(insertsql) - 2) & "')"
' adderar posten
Con.Execute insertsql, , adCmdText + adExecuteNoRecords ' = 129
Loop
Close #1Sv: Läsa sista raden i en textfil
Med din kod fungerar det perfekt, nu kommer alla raderna med.
Mvh
BosseSv: Läsa sista raden i en textfil
<code>
Public Sub CreateTable()
Dim Buffer As String,vArray() As String
Dim FileNum As Long,i As Long
FileNum = FreeFile
' hämtar lite värden
FileName = Form1.txtFileName
TableName = Form1.txtTable
' öppnar filen
Open FileName For Input As #FileNum
orgsql = "insert into " & TableName & _
" (BDATE,BTIME,BITNO,BFUT,BFIN,BAVD,BANSV,BMASK,BPROJ,BFMAN) values("
Do Until EOF(FileNum)
Line Input #FileNum, Buffer
vArray = Split(Buffer, "|")
insertsql = orgsql
For i = 0 To 9
' alla värden
insertsql = insertsql & "'" & vArray(i) & "',"
Next 'i
' tar bort avslutande "
insertsql = Left(insertsql, Len(insertsql) - 2) & "')"
' adderar posten
Con.Execute insertsql, , adCmdText + adExecuteNoRecords ' = 129
Loop
Close #FileNum
End Sub
</code>