Hi there,
Can someone please look at my code and help me out help.
I am trying to convert over VB code to work with VBA in Access 2003. The code to list all of the available text files in a particular folder is working fine, and they get populated in a listbox called lstFiles, the problem is when I click on one of the textfile from this listbox I want it to show the contents of the text file in the listbox below named lstFileLines. I get the Compile Error: Method or data member not found, and highlights Text from lstFiles.Text on the following line of code:
Set oFile = fso.GetFile(txtFolder.Text & lstFiles.Text)
Here is all of my code, I am ultimately trying to import this text file into a table named CardTest, in case you notice any other code errors that I may have.
Option Compare Database
Private Sub cmdClear_Click()
Me.lstFileLines = ""
Me.lstFiles = ""
End Sub
Private Sub cmdClose_Click()
DoCmd.Close acForm, "frmImportCards", acSaveYes
End Sub
Private Sub cmdGetFiles_Click()
'Declaring the following variables
Dim fso As Scripting.FileSystemObject
Dim oFolder As Scripting.Folder
Dim oFile As Scripting.File
Dim i As Long
'Instantiating the filesystem object
Set fso = New Scripting.FileSystemObject
'Clearing the file list
'Me.lstFiles = ""
txtFolder.SetFocus
txtFolder.Text = "E:\Cards\1980-81\O-Pee-Chee"
If Len(txtFolder.Text) > 0 Then
If Right(txtFolder.Text, 1) <> "\" Then
txtFolder.Text = txtFolder.Text & "\"
End If
'Checking to see if the folder name typed in the textbox actually exists
If fso.FolderExists(txtFolder.Text) Then
'Setting the oFolder object to what is in the textbox
Set oFolder = fso.GetFolder(txtFolder.Text)
'Looping through the files in the folder object
For Each oFile In oFolder.Files
'Checking the file extension of the file
If Len(oFile.Name) >= 4 Then
'If the file has a txt file extension then add the files
If UCase(Right(oFile.Name, 4)) = ".TXT" Then
lstFiles.AddItem oFile.Name
End If
End If
Next
Else
MsgBox "This folder does not exist."
End If
End If
'Removing the oFile object from memory
Set oFile = Nothing
Set oFolder = Nothing
Set fso = Nothing
End Sub
Private Sub cmdImport_Click()
Dim i As Long, iPos As Long
Dim sLine As String
Dim sCardNo As String, sFirstName As String, sLastName As String
Dim sRookieCard As String, sAdditionalDescription As String
Dim sYearId As String, sSetId As String, sSubsetId As String
Me.Refresh
DoEvents
If lstFileLines.ListCount > 0 Then
m_oConnection.Open
If m_oConnection.State = adStateOpen Then
sSetId = InputBox("What is the Set #?", "Set", "0")
sSubsetId = InputBox("What is the Subset #?", "Set", "0")
sYearId = InputBox("What is the Year Id#?", "Set", "0")
For i = 1 To lstFileLines.ListCount
sLine = lstFileLines.ListCount(i - 1)
Debug.Print sLine
iPos = InStr(1, sLine, Chr(9))
If iPos > 0 Then
sCardNo = Mid(sLine, 1, iPos - 1)
sLine = Right(sLine, Len(sLine) - iPos)
iPos = InStr(1, sLine, Chr(9))
If iPos > 0 Then
sFirstName = Mid(sLine, 1, iPos - 1)
sLine = Right(sLine, Len(sLine) - iPos)
iPos = InStr(1, sLine, Chr(9))
If iPos > 0 Then
sLastName = Mid(sLine, 1, iPos - 1)
sLine = Right(sLine, Len(sLine) - iPos)
iPos = InStr(1, sLine, Chr(9))
If iPos > 0 Then
sRookieCard = Mid(sLine, 1, iPos - 1)
sLine = Right(sLine, Len(sLine) - iPos)
iPos = InStr(1, sLine, Chr(9))
sAdditionalDescription = Replace(sLine, """", """""")
End If
End If
End If
End If
ImportRecord sCardNo, sFirstName, sLastName, sRookieCard, sAdditionalDescription, sSetId, sSubsetId, sYearId
Next i
m_oConnection.Close
End If
End If
End Sub
'Private Sub Form_Load()
'm_sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Cards\AnotherTry.mdb"
'Set m_Connection = New ADODB.Connection
'm_oConnection.ConnectionString = m_sConnectionString
'End Sub
Private Sub lstFiles_Click()
Dim fso As Scripting.FileSystemObject
Dim oStream As Scripting.TextStream
Dim oFile As Scripting.File
Set fso = New Scripting.FileSystemObject
Set oFile = fso.GetFile(txtFolder.Text & lstFiles.Text)
Set oStream = oFile.OpenAsTextStream(ForReading)
'Me.lstFileLines = ""
Do While Not oStream.AtEndOfStream
lstFileLines.AddItem oStream.ReadLine
Loop
oStream.Close
If lstFileLines.ListCount > 0 Then
cmdImport.Enabled = True
End If
End Sub
Private Sub ImportRecord(ByVal sCardNo As String, ByVal sFirstName As String, ByVal sLastName As String, ByVal sRookieCard As String, ByVal sAdditionalDescription As String, ByVal SetId As String, ByVal SubsetId As String, ByVal sYearId As String)
Dim sSQL As String
Dim iSetId As Integer
Dim iSubsetId As Integer
Dim iYearId As Integer
iSetId = Val(sSetId)
iSubsetId = Val(sSubsetId)
iYearId = Val(sYearId)
If Len(sCardNo) <> 0 And Len(sFirstName) <> 0 And Len(sLastName) <> 0 Then
sSQL = "INSET INTO CARDTEST (CardNo, FirstName, LastName, RookieCard, AdditionalDescription, SetId, SubsetId, YearId)VALUES (""" & sCardNo & """, """ & sFirstName & """, """ & sLastName & """, """ & sRookieCard & """, """ & sAdditionalDescription & """, " & iSetId & ", " & iSubsetId & ", " & iYearId & ")"
m_oConnection.Execute sSQL
End If
Debug.Print sSQL
End Sub
Thanks
Can someone please look at my code and help me out help.
I am trying to convert over VB code to work with VBA in Access 2003. The code to list all of the available text files in a particular folder is working fine, and they get populated in a listbox called lstFiles, the problem is when I click on one of the textfile from this listbox I want it to show the contents of the text file in the listbox below named lstFileLines. I get the Compile Error: Method or data member not found, and highlights Text from lstFiles.Text on the following line of code:
Set oFile = fso.GetFile(txtFolder.Text & lstFiles.Text)
Here is all of my code, I am ultimately trying to import this text file into a table named CardTest, in case you notice any other code errors that I may have.
Option Compare Database
Private Sub cmdClear_Click()
Me.lstFileLines = ""
Me.lstFiles = ""
End Sub
Private Sub cmdClose_Click()
DoCmd.Close acForm, "frmImportCards", acSaveYes
End Sub
Private Sub cmdGetFiles_Click()
'Declaring the following variables
Dim fso As Scripting.FileSystemObject
Dim oFolder As Scripting.Folder
Dim oFile As Scripting.File
Dim i As Long
'Instantiating the filesystem object
Set fso = New Scripting.FileSystemObject
'Clearing the file list
'Me.lstFiles = ""
txtFolder.SetFocus
txtFolder.Text = "E:\Cards\1980-81\O-Pee-Chee"
If Len(txtFolder.Text) > 0 Then
If Right(txtFolder.Text, 1) <> "\" Then
txtFolder.Text = txtFolder.Text & "\"
End If
'Checking to see if the folder name typed in the textbox actually exists
If fso.FolderExists(txtFolder.Text) Then
'Setting the oFolder object to what is in the textbox
Set oFolder = fso.GetFolder(txtFolder.Text)
'Looping through the files in the folder object
For Each oFile In oFolder.Files
'Checking the file extension of the file
If Len(oFile.Name) >= 4 Then
'If the file has a txt file extension then add the files
If UCase(Right(oFile.Name, 4)) = ".TXT" Then
lstFiles.AddItem oFile.Name
End If
End If
Next
Else
MsgBox "This folder does not exist."
End If
End If
'Removing the oFile object from memory
Set oFile = Nothing
Set oFolder = Nothing
Set fso = Nothing
End Sub
Private Sub cmdImport_Click()
Dim i As Long, iPos As Long
Dim sLine As String
Dim sCardNo As String, sFirstName As String, sLastName As String
Dim sRookieCard As String, sAdditionalDescription As String
Dim sYearId As String, sSetId As String, sSubsetId As String
Me.Refresh
DoEvents
If lstFileLines.ListCount > 0 Then
m_oConnection.Open
If m_oConnection.State = adStateOpen Then
sSetId = InputBox("What is the Set #?", "Set", "0")
sSubsetId = InputBox("What is the Subset #?", "Set", "0")
sYearId = InputBox("What is the Year Id#?", "Set", "0")
For i = 1 To lstFileLines.ListCount
sLine = lstFileLines.ListCount(i - 1)
Debug.Print sLine
iPos = InStr(1, sLine, Chr(9))
If iPos > 0 Then
sCardNo = Mid(sLine, 1, iPos - 1)
sLine = Right(sLine, Len(sLine) - iPos)
iPos = InStr(1, sLine, Chr(9))
If iPos > 0 Then
sFirstName = Mid(sLine, 1, iPos - 1)
sLine = Right(sLine, Len(sLine) - iPos)
iPos = InStr(1, sLine, Chr(9))
If iPos > 0 Then
sLastName = Mid(sLine, 1, iPos - 1)
sLine = Right(sLine, Len(sLine) - iPos)
iPos = InStr(1, sLine, Chr(9))
If iPos > 0 Then
sRookieCard = Mid(sLine, 1, iPos - 1)
sLine = Right(sLine, Len(sLine) - iPos)
iPos = InStr(1, sLine, Chr(9))
sAdditionalDescription = Replace(sLine, """", """""")
End If
End If
End If
End If
ImportRecord sCardNo, sFirstName, sLastName, sRookieCard, sAdditionalDescription, sSetId, sSubsetId, sYearId
Next i
m_oConnection.Close
End If
End If
End Sub
'Private Sub Form_Load()
'm_sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Cards\AnotherTry.mdb"
'Set m_Connection = New ADODB.Connection
'm_oConnection.ConnectionString = m_sConnectionString
'End Sub
Private Sub lstFiles_Click()
Dim fso As Scripting.FileSystemObject
Dim oStream As Scripting.TextStream
Dim oFile As Scripting.File
Set fso = New Scripting.FileSystemObject
Set oFile = fso.GetFile(txtFolder.Text & lstFiles.Text)
Set oStream = oFile.OpenAsTextStream(ForReading)
'Me.lstFileLines = ""
Do While Not oStream.AtEndOfStream
lstFileLines.AddItem oStream.ReadLine
Loop
oStream.Close
If lstFileLines.ListCount > 0 Then
cmdImport.Enabled = True
End If
End Sub
Private Sub ImportRecord(ByVal sCardNo As String, ByVal sFirstName As String, ByVal sLastName As String, ByVal sRookieCard As String, ByVal sAdditionalDescription As String, ByVal SetId As String, ByVal SubsetId As String, ByVal sYearId As String)
Dim sSQL As String
Dim iSetId As Integer
Dim iSubsetId As Integer
Dim iYearId As Integer
iSetId = Val(sSetId)
iSubsetId = Val(sSubsetId)
iYearId = Val(sYearId)
If Len(sCardNo) <> 0 And Len(sFirstName) <> 0 And Len(sLastName) <> 0 Then
sSQL = "INSET INTO CARDTEST (CardNo, FirstName, LastName, RookieCard, AdditionalDescription, SetId, SubsetId, YearId)VALUES (""" & sCardNo & """, """ & sFirstName & """, """ & sLastName & """, """ & sRookieCard & """, """ & sAdditionalDescription & """, " & iSetId & ", " & iSubsetId & ", " & iYearId & ")"
m_oConnection.Execute sSQL
End If
Debug.Print sSQL
End Sub
Thanks