Link to home
Start Free TrialLog in
Avatar of isurgyn
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("[PatientFolderPath]", "tblPathNames", 1)
    strPatientID = Forms!frmPatientDemographicData!PatientID
    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.filesystemobject")
   
    For Each FileInFromFolder In fso.getfolder(FromPath).Files
   
        FileName = (FileInFromFolder.Name)
        FileDate = (FileInFromFolder.DateLastModified)

            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
Avatar of Ken Butters
Ken Butters
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of isurgyn
isurgyn

ASKER

Might be the easiest points you ever got but THANK YOU!  This is my first attempt a creating a rst from scratch and just didn't think about Msgbox syntax.
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?  

:)