isurgyn
asked on
Adding multiple rows to ADO recordset using VBA in Access 2007
Hi,
I am likely missing something pretty obvious but here goes. I am attempting to create an ADO disconnected recordset that contains the File Name and Date Created Data from a folder on our server. I am also attempting to create a column in the recordset for indexing called File count as well as adding two boolean columns for use later when the user would like to print and / or email / fax the document.
The code loops through the files properly and the recordset appears to have the correct file count but each row in the recordset appears to always be the File Name and Date Created data from the first file. The data from the next file(s) are not added to the array. Also, the FileNID on this first row in the recordset is 0 which doesn't make sense as the FileCount variable should be 1.
My thinking is that perhaps there is a mismatch on data type either on the FileNID index or on Date. However, the first row in the rst appears correct, except for the FileNID being incorrect, so I just don't know where I have screwed up. Maybe I don't need the FileNID column and should eliminate it.
Thanks
Dim fso As Object
Dim FromPath As String
Dim FileName As String
Dim FullPath As String
Dim FileDate As Date
Dim FileInFromFolder As Object
Dim FileCount As Integer
Dim FileNID As Integer
Dim Prt As Boolean
Dim Fx As Boolean
Dim strMsg As String
Dim strPatientID As String
Dim iResponse As Integer
Dim rst2 As ADODB.Recordset
FileCount = 0
On Error GoTo ErrHandler
FromPath = DLookup("[PatientFolderPat h]", "tblPathNames", 1)
strPatientID = Forms!frmPatientDemographi cData!Pati entID
FromPath = FromPath & strPatientID
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
Set rst2 = New ADODB.Recordset
With rst2
Set .ActiveConnection = Nothing
.CursorLocation = adUseClient
With .Fields
.Append "FileNID", adInteger
.Append "FileName", adVarChar, 255
.Append "FileDate", adDate
.Append "Prt", adBoolean
.Append "Fx", adBoolean
End With
.Open
Set fso = CreateObject("scripting.fi lesystemob ject")
For Each FileInFromFolder In fso.getfolder(FromPath).Fi les
FileName = (FileInFromFolder.Name)
FileDate = (FileInFromFolder.DateLast Modified)
FileCount = FileCount + 1
FullPath = FromPath & FileName
.AddNew Array("FileNID", "FileName", "FileDate", "Prt", "Fx"), _
Array(FileCount, FullPath, FileDate, 0, 0)
Next FileInFromFolder
.MoveFirst
Do Until .EOF
MsgBox "FileNID = " & FileNID & " FileName =" & FullPath & " FileDate =" & FileDate & " Print =" & Prt & " Fax = " & Fx
.MoveNext
Loop
.Close
End With
I am likely missing something pretty obvious but here goes. I am attempting to create an ADO disconnected recordset that contains the File Name and Date Created Data from a folder on our server. I am also attempting to create a column in the recordset for indexing called File count as well as adding two boolean columns for use later when the user would like to print and / or email / fax the document.
The code loops through the files properly and the recordset appears to have the correct file count but each row in the recordset appears to always be the File Name and Date Created data from the first file. The data from the next file(s) are not added to the array. Also, the FileNID on this first row in the recordset is 0 which doesn't make sense as the FileCount variable should be 1.
My thinking is that perhaps there is a mismatch on data type either on the FileNID index or on Date. However, the first row in the rst appears correct, except for the FileNID being incorrect, so I just don't know where I have screwed up. Maybe I don't need the FileNID column and should eliminate it.
Thanks
Dim fso As Object
Dim FromPath As String
Dim FileName As String
Dim FullPath As String
Dim FileDate As Date
Dim FileInFromFolder As Object
Dim FileCount As Integer
Dim FileNID As Integer
Dim Prt As Boolean
Dim Fx As Boolean
Dim strMsg As String
Dim strPatientID As String
Dim iResponse As Integer
Dim rst2 As ADODB.Recordset
FileCount = 0
On Error GoTo ErrHandler
FromPath = DLookup("[PatientFolderPat
strPatientID = Forms!frmPatientDemographi
FromPath = FromPath & strPatientID
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
Set rst2 = New ADODB.Recordset
With rst2
Set .ActiveConnection = Nothing
.CursorLocation = adUseClient
With .Fields
.Append "FileNID", adInteger
.Append "FileName", adVarChar, 255
.Append "FileDate", adDate
.Append "Prt", adBoolean
.Append "Fx", adBoolean
End With
.Open
Set fso = CreateObject("scripting.fi
For Each FileInFromFolder In fso.getfolder(FromPath).Fi
FileName = (FileInFromFolder.Name)
FileDate = (FileInFromFolder.DateLast
FileCount = FileCount + 1
FullPath = FromPath & FileName
.AddNew Array("FileNID", "FileName", "FileDate", "Prt", "Fx"), _
Array(FileCount, FullPath, FileDate, 0, 0)
Next FileInFromFolder
.MoveFirst
Do Until .EOF
MsgBox "FileNID = " & FileNID & " FileName =" & FullPath & " FileDate =" & FileDate & " Print =" & Prt & " Fax = " & Fx
.MoveNext
Loop
.Close
End With
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Lol... well the final code change was easy... but the process was not as quick... copying all the code into Access... accounting for missing routines / tables etc... getting set up to trace...finding the right libraries to include, tracing through each step... and arriving at the bottom and scratching my head for a few minutes....saying... what the what?
:)
:)
ASKER