Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329

    Recordset criterias

    Hi Guys, can anyone help with this one, I use recordsets quite frequently, sometimes I have various criterias in a recordset but usually different fields ie: date, numerical, name etc, I am trying to retrieve data where the same field may equal 2 different words, so I am trying put x 2 criterias in the recordset where the same field = either 2 words from 4 ie,
    I have a Status field that can be planning-delivery-collection-on hold, the following is what I have got but sends access to "not responding"

    Dim rs as DAO.Recordset
    Dim MyBody, MySt1, MySt2 as String



    MySt1 = "Planning"
    MySt2 = "On Hold"

    Set rs = Currentdb.OpenRecordset("Select * From tblEdit WHERE Status = '" & MySt1 & "'" & " And Status = '" & Myst2 & "'")
    Do Until rs.EOF

    rs.MoveFirst

    MyBody = MyBody & rs.Fields("Name") & " - " & rs.Fields("Status") & vbNewLine

    rs.MoveNext

    Loop

    MsgBox(rs.RecordCount & " " & "Records Found With On Hold And Planning" & Chr(10) & Chr(10) & _
    MyBody)

    Is my recordset correct ? do I need the word "Or" putting in instead of 2 strings ?

    Much appreciated Dave

  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,127
    Replace And with Or in your Set Rs line
    You also need to place Do Until after MoveFirst
    Changes/additions in RED

    Code:
    Dim rs as DAO.Recordset
    Dim MyBody As String, MySt1 As String, MySt2 as String
    
    MySt1 = "Planning"
    MySt2 = "On Hold"
    
    Set rs = Currentdb.OpenRecordset("Select * From tblEdit WHERE Status = '" & MySt1 & "'" & " Or Status = '" & Myst2 & "'")
    
    With rs
      .MoveFirst
       Do Until .EOF
       MyBody = MyBody & rs.Fields("Name") & " - " & rs.Fields("Status") & vbNewLine
       .MoveNext
       Loop
    
      MsgBox(.RecordCount & " " & "Records Found With " & MySt1 & " And " & MySt2 & Chr(10) & Chr(10) & MyBody)
      
      .Close
    End With
    
    Set rs=Nothing
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  3. #3
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329
    Hi ridders52, thank you for your reply, i'm not sure what you mean, would you kindly write that line ?? regards Dave

  4. #4
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329
    I think o know what you mean, I will try and let you know thanks for your help will comment later

  5. #5
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329
    I have tried that and still won't return the records ?? I know I have 2 records with MySt1 and 3 Records with MySt2, was hoping it would return them on the MsgBox to confirm but wont !!! regards Dave

  6. #6
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,127
    I just updated it with the code while you were replying
    Tested on a similar example here.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  7. #7
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329
    Thanks ridders52, will give that a go much appreciated, will reply in a while and leave you comment

  8. #8
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,127
    One further thing

    Code:
    Dim MyBody, MySt1, MySt2 as String
    causes MyBody, MySt1 to be variants

    It should be
    Code:
    Dim MyBody As String, MySt1 As String, MySt2 as String
    I've corrected my code sample
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  9. #9
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329
    Hi ridders52, all worked a treat thank you, I clicked the star and posted excellent comment

    Is there an easy method of creating a backup of front end and backend db ? , I will post where I got with that one, I tend to 80% do things and get side tracked!!

  10. #10
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329
    Dim myFile, myBEPath, BackupFile, BackupPath As String
    myBEPath = "\T:\DMT\DB Backend"
    myFile = "DMT Live_be" & ".accdb"
    BackupPath = "\T:\DMT\DB Backup"
    BackupFile = "DMT Live_be" & ".accdb" & " " & Format(Now(), "dd-mm-yy")
    FileCopy myPath & myFile, BackupPath & BackupFile

  11. #11
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,127
    A few errors with backslashes in your code
    a) there shouldn't be a backslash before T:\
    b) there should be a backslash between file path and file name

    Code:
    Dim myFile, myBEPath, BackupFile, BackupPath As String
    myBEPath = "T:\DMT\DB Backend"
    myFile = "DMT Live_be" & ".accdb"
    BackupPath = "T:\DMT\DB Backup"
    BackupFile = "DMT Live_be" & ".accdb" & " " & Format(Now(), "dd-mm-yy")
    FileCopy myPath & "\" & myFile, BackupPath & "\" & BackupFile
    However you may find the following two functions more useful -they are similar to the functions I use
    These backup the FE or BE database to a specified folder and show a message with the backup file name/location/size
    In each case, the file is first copied to a temp file then compacted before copying the compacted file to the final location

    1. Backup BE file
    Code:
    Public Function BackupBEDatabase()
    
    On Error GoTo Err_Handler
    
    'creates a copy of the backend database to the backups folder with date/time suffix
        Dim fso As Object
        Dim strOldPath As String, strNewPath As String, strTempPath As String, strFileSize As String
        Dim newlength As Long
        Dim STR_PASSWORD As String
        
        'if your BE database is password protected, enter it below or state where it can be found
       ' STR_PASSWORD = ""    'enter password
       ' STR_PASSWORD = Nz(DLookup("ItemValue", "tblProgramSettings", "ItemName='Pwd'"), "") 'example for stored password 
        
        Set fso = CreateObject("Scripting.FileSystemObject")
        
        strFilename = "UKAddressFinderBE.accdb" 'replace with your BE file
        strFileType = Mid(strFilename, InStr(strFilename, ".")) 'e.g. .accdb
                
        strOldPath = GetLinkedDBFolder & "\" & strFilename 'replace GetLinkedDBFolder with your BE folder
        
        'replace GetBackupsFolder with your backups folder
        strNewPath = GetBackupsFolder & "\BE\" & Left(strFilename, InStr(strFilename, ".") - 1) & "_" & Format(Now, "yyyymmddhhnnss") & strFileType
        
       'set path for temp file    
        strTempPath = GetBackupsFolder & "\" & Left(strFilename, InStr(strFilename, ".") - 1) & "_TEMP" & strFileType
             
       ' Debug.Print strOldPath
       ' Debug.Print strTempPath
       ' Debug.Print strNewPath
           
         If MsgBox("This procedure is used to make a backup copy of the back end database" & vbCrLf & _
                "The backup will be saved to the Backups folder with date/time suffix" & vbCrLf & _
                    vbTab & "e.g. " & strNewPath & "                            " & vbCrLf & vbCrLf & _
                "This can be used for recovery in case of problems    " & vbCrLf & vbCrLf & _
                "Create a backup now?", _
                    vbExclamation + vbYesNo, "Copy the Access BE database?") = vbNo Then
                        Exit Function
            Else
                'copy database to a temp file
                fso.CopyFile strOldPath, strTempPath
                Set fso = Nothing
                          
                'compact the temp file (with password)
                DBEngine.CompactDatabase strTempPath, strNewPath, ";PWD=" & STR_PASSWORD & "", , ";PWD=" & STR_PASSWORD & ""
                'OR compact the temp file code if no password 
                'DBEngine.CompactDatabase strTempPath, strNewPath    
                
                'delete the tempfile
                Kill strTempPath
                    
                DoEvents
                
                'get size of backup
                newlength = FileLen(strNewPath) 'in bytes
                 
                'setup string to display file size
                If FileLen(strNewPath) < 1024 Then  'less than 1KB
                   strFileSize = newlength & " bytes"
                ElseIf FileLen(strNewPath) < 1024 ^ 2 Then  'less than 1MB
                   strFileSize = Round((newlength / 1024), 0) & " KB"
                ElseIf newlength < 1024 ^ 3 Then 'less than 1GB
                   strFileSize = Round((newlength / 1024), 0) & " KB   (" & Round((newlength / 1024 ^ 2), 1) & " MB)"
                Else 'more than 1GB
                    strFileSize = Round((newlength / 1024), 0) & " KB   (" & Round((newlength / 1024 ^ 3), 2) & " GB)"
                End If
                
                DoEvents
                
        End If
                
        MsgBox "The Access backend database has been successfully backed up.                " & _
            "The backup file is called " & vbCrLf & vbTab & strNewPath & vbCrLf & vbCrLf & _
                "The file size is " & strFileSize, vbInformation, "Access BE Backup completed"
                   
    Exit_Handler:
        Exit Function
        
    Err_Handler:
        Set fso = Nothing
        If Err <> 0 Then
          MsgBox "Error " & Err.Number & " in BackupBEDatabase procedure : " & _
             Err.description, vbCritical, "Error copying database"
        End If
        Resume Exit_Handler
        
    End Function
    2. Backup FE file
    Code:
    Public Function BackupFEDatabase()
    
    On Error GoTo Err_Handler
    
    'creates a copy of the current db (frontend) to the backups folder with date/time suffix
        Dim fso As Object
        Dim strOldPath As String, strNewPath As String, strTempPath As String, strFileSize As String
        Dim newlength As Long
        
        Set fso = CreateObject("Scripting.FileSystemObject")
        
        strFileType = Mid(CurrentDb.Name, InStr(CurrentDb.Name, ".")) 'e.g. .accdb
        strFilename = Mid(CurrentDb.Name, InStrRev(CurrentDb.Name, "\") + 1)
        strFilename = Left(strFilename, Len(strFilename) - Len(strFileType)) 
        
        strOldPath = CurrentDb.Name
        strTempPath = Left(CurrentDb.Name, InStr(CurrentDb.Name, ".") - 1) & "_TEMP" & Mid(CurrentDb.Name, InStr(CurrentDb.Name, "."))          
        strNewPath = GetBackupsFolder & "\FE\" & strFilename & "_" & Format(Now, "yyyymmddhhnnss") & strFileType
        
            If MsgBox("This procedure is used to make a backup copy of the front end (FE) database." & vbCrLf & _
                "The backup will be saved to the Backups folder with date/time suffix" & vbCrLf & vbTab & "e.g. " & strNewPath & vbCrLf & vbCrLf & _
                "This can be used for recovery in case of problems    " & vbCrLf & vbCrLf & _
                "Create a backup now?", _
                    vbExclamation + vbYesNo, "Copy the Access FE database?") = vbNo Then
                        Exit Function
            Else
                'copy database to a temp file
                fso.CopyFile strOldPath, strTempPath
                Set fso = Nothing
                
                strNewPath = GetBackupsFolder & "\FE\" & strFilename & "_" & Format(Now, "yyyymmddhhnnss") & strFileType
                    
                'Debug.Print strTempPath
                'Debug.Print strNewPath
                
                'compact the temp file
                DBEngine.CompactDatabase strTempPath, strNewPath
                
                'delete the tempfile
                Kill strTempPath
                    
                DoEvents
                
                'get size of backup
                newlength = FileLen(strNewPath) 'in bytes
                 
                'setup string to display file size
                If FileLen(strNewPath) < 1024 Then  'less than 1KB
                   strFileSize = newlength & " bytes"
                ElseIf FileLen(strNewPath) < 1024 ^ 2 Then  'less than 1MB
                   strFileSize = Round((newlength / 1024), 0) & " KB"
                ElseIf newlength < 1024 ^ 3 Then 'less than 1GB
                   strFileSize = Round((newlength / 1024), 0) & " KB   (" & Round((newlength / 1024 ^ 2), 1) & " MB)"
                Else 'more than 1GB
                    strFileSize = Round((newlength / 1024), 0) & " KB   (" & Round((newlength / 1024 ^ 3), 2) & " GB)"
                End If
                
                DoEvents
                
        End If
                
        MsgBox "The Access FE database has been successfully backed up.                " & _
            "The backup file is called " & vbCrLf & vbTab & strNewPath & vbCrLf & vbCrLf & _
                "The file size is " & strFileSize, vbInformation, "Access FE Backup completed"
                    
    Exit_Handler:
        Exit Function
        
    Err_Handler:
        Set fso = Nothing
        If Err <> 0 Then
          MsgBox "Error " & Err.Number & " in BackupFEDatabase procedure : " & _
              Err.description, vbCritical, "Error copying database"
        End If
        Resume Exit_Handler
        
    End Function
    Example message on completion:

    Click image for larger version. 

Name:	Capture.PNG 
Views:	22 
Size:	6.6 KB 
ID:	34122
    Last edited by isladogs; 05-20-2018 at 07:26 AM. Reason: Added screenshot & removed extraneous code
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  12. #12
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329
    Hi ridders52, thank you very much, I need to convert your method to the paths and names of mine and give it a try, maybe wise if I manually copy and paste my front and backend in case I mess up, I have seen your Instring code before but not used so will have a mess about tomorrow at work and try to get backups done, will leave comment soon as done.. regards Dave

  13. #13
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,127
    You're welcome.
    Whilst it's a good idea to make a copy just in case whilst testing, the backup process doesn't delete your original file so it's very unlikely to cause issues.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  14. #14
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329
    Hi ridders52, I haven't had time to try your backing up method, will hopefully get a chance tomorrow, additional to backup, I have something like 5900 records in one table and as we change status, I have recordsets based on status to move that record to the relevant table, my main table tblEdit, this the table with 5900 + records, is there a limit to how many records can be added ? also integrating your backup method, can I for example move all records to an external database to free up the table tblEdit but still access the external table for searching ?, hope this makes sense kind regards dave

  15. #15
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,127
    Quote Originally Posted by DMT Dave View Post
    Hi ridders52, I haven't had time to try your backing up method, will hopefully get a chance tomorrow, additional to backup, I have something like 5900 records in one table and as we change status, I have recordsets based on status to move that record to the relevant table, my main table tblEdit, this the table with 5900 + records, is there a limit to how many records can be added ? also integrating your backup method, can I for example move all records to an external database to free up the table tblEdit but still access the external table for searching ?, hope this makes sense kind regards dave
    OK several things here:
    1. Moving 5900+ records to another table would be far quicker using an append query followed by a delete query as these effectively act on all records at once whereas a recordset loops through one record at a time.

    2. Access does have limits but the number of records in a table isn't one of them as long as you don't exceed the 2GB file size limit.
    I have several tables with almost 3 million records. Searching can be slow with very large tables but indexing solves that issue

    This article tells you what limits there are in Access: https://support.office.com/en-us/art...8-98c1025bb47c

    3. Yes you can move records to an external database with a linked table. The linked table will behave exactly as any local table. It can be searched or edited from the main database

    4. Having said all that, why do you need to move the records at all? Why not keep them in the same table and use the status field (or similar) to mark them as e.g. archived
    One of my biggest mistakes was creating separate tables for student leavers and yet more tables for archived records. The amount of additional work this caused over the years was enormous and I strongly advise against doing anything like that
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. query to get data from 2 criterias
    By megatronixs in forum Queries
    Replies: 2
    Last Post: 05-06-2016, 12:00 AM
  2. How to search for multiple criterias using comboBox?
    By AccessPractice in forum Programming
    Replies: 2
    Last Post: 04-29-2016, 03:59 AM
  3. Replies: 3
    Last Post: 05-07-2015, 10:24 AM
  4. Query based on two different criterias
    By BRZ-Ryan in forum Queries
    Replies: 11
    Last Post: 12-22-2013, 09:25 PM
  5. search a value with 2 criterias
    By Patougaffou in forum Queries
    Replies: 5
    Last Post: 07-26-2011, 10:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums