Here is what I have so far:
Private Sub cmdOpenFile_Click()
On Error GoTo Err_cmdOpenFile_Click
Dim strLine As String
Dim intLength As Integer
Dim intPos As Integer
Dim strSpace As String
Dim strSerialNumber As String
Dim strSQL As String
Dim strHostName As String
Dim strTemp As String
Dim strFileName As String
dlgCommon.Filter = ("Text Files (*.txt) | *.txt"
strEquals = "="
dlgCommon.ShowOpen
Open dlgCommon.FileName For Input As #1
Do
Input #1, strLine
Loop Until Left(strLine, 13) = "Serial Number"
'sets the serial number from file
intLength = Len(strLine) 'checks length of string
intPos = InStr(strLine, strEquals) 'checks for "=" sign
strSerialNumber = Right(strLine, (intLength - (intPos + 1))) ' gets all characters after "=" sign
'sets the hostname from file
intLength = Len(dlgCommon.FileName) 'Gets length of file and path
strTemp = Right(dlgCommon.FileName, (intLength - 18)) 'truncates the path from file name
intLength = Len(strTemp) 'gets length of file name
strHostName = Left(strTemp, (intLength - 4)) 'truncates extension from file name
txtSerialNum = strSerialNumber
txtHostName = strHostName
strSQL = "INSERT INTO [tblserialnum] values ('" & strSerialNumber & "', '" & strHostName & "')"
DoCmd.RunSQL strSQL
Close #1
Exit Sub
Err_cmdOpenFile_Click:
Close #1
If Err.Number <> 32755 Then
MsgBox Err.Description, vbOKOnly, "Error " & Err.Number
End If
End Sub
What I would like to do is eliminate having to open the file manually. How can I loop through a directory (ex. m:\logs) and get each file name, stuff it in a variable the have the code open and use that. When it's through have it loop again and read the next file name in the directory.
Private Sub cmdOpenFile_Click()
On Error GoTo Err_cmdOpenFile_Click
Dim strLine As String
Dim intLength As Integer
Dim intPos As Integer
Dim strSpace As String
Dim strSerialNumber As String
Dim strSQL As String
Dim strHostName As String
Dim strTemp As String
Dim strFileName As String
dlgCommon.Filter = ("Text Files (*.txt) | *.txt"
strEquals = "="
dlgCommon.ShowOpen
Open dlgCommon.FileName For Input As #1
Do
Input #1, strLine
Loop Until Left(strLine, 13) = "Serial Number"
'sets the serial number from file
intLength = Len(strLine) 'checks length of string
intPos = InStr(strLine, strEquals) 'checks for "=" sign
strSerialNumber = Right(strLine, (intLength - (intPos + 1))) ' gets all characters after "=" sign
'sets the hostname from file
intLength = Len(dlgCommon.FileName) 'Gets length of file and path
strTemp = Right(dlgCommon.FileName, (intLength - 18)) 'truncates the path from file name
intLength = Len(strTemp) 'gets length of file name
strHostName = Left(strTemp, (intLength - 4)) 'truncates extension from file name
txtSerialNum = strSerialNumber
txtHostName = strHostName
strSQL = "INSERT INTO [tblserialnum] values ('" & strSerialNumber & "', '" & strHostName & "')"
DoCmd.RunSQL strSQL
Close #1
Exit Sub
Err_cmdOpenFile_Click:
Close #1
If Err.Number <> 32755 Then
MsgBox Err.Description, vbOKOnly, "Error " & Err.Number
End If
End Sub
What I would like to do is eliminate having to open the file manually. How can I loop through a directory (ex. m:\logs) and get each file name, stuff it in a variable the have the code open and use that. When it's through have it loop again and read the next file name in the directory.