Results 1 to 7 of 7
  1. #1
    mlrucci is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Apr 2018
    Posts
    202

    export to excel not including all records

    Good morning, here is one for you. I have racked my brain and cannot figure this one out. I have vba code to export a query using a parameter from a combo box on a form. In order for the code to work, I have to use the Eval function. Works great but having difficulty with exporting a record that has a false (checkbox) result. The information in the query works, it is the export that is having the problem. I believe it has to do with the function eval. From my understanding, it used to evaluate an expression that results in a text string or a numeric value. I am baffled. I tried to change the criteria in the query to include both true and false, no resolution.



    query:

    Code:
    SELECT lnkComponentToMCField.ComponentName, lnkComponentToMCField.MCFieldName, lnkComponentToMCField.IsComponentFieldRequired, lkpValidationType.ValidationType, lnkComponentToMCField.[Audit Notes] AS AuditNotes, lnkComponentToMCField.ComponentValidationCondition, lnkComponentToMCField.ValidationTypeID
    FROM lnkComponentToMCField INNER JOIN lkpValidationType ON lnkComponentToMCField.ValidationTypeID = lkpValidationType.ValidationTypeID
    WHERE (((lnkComponentToMCField.ComponentName)=Eval("[Forms]![frmNavMain]![frmNavMain].[Form]![frmNavMgt].[Form]![cboCompName]")) AND ((lnkComponentToMCField.IsComponentFieldRequired)=True Or (lnkComponentToMCField.IsComponentFieldRequired)=False)) OR (((Eval("[Forms]![frmNavMain]![frmNavMain].[Form]![frmNavMgt].[Form]![cboCompName]")) Is Null))
    ORDER BY lnkComponentToMCField.ComponentName;


    VBA:
    Code:
    Private Sub cmdExport_Click()
        Call Export2XLS("SELECT ComponentName, MCFieldName, IsComponentFieldRequired, ValidationType, AuditNotes, ComponentValidationCondition FROM qryMgtComponant_MCField")
    End Sub
    
    
    Function Export2XLS(ByVal sQuery As String)
    Const xlCenter = -4108
    
    On Error Resume Next
        Set oExcel = GetObject(, "Excel.Application")    'Bind to existing instance of Excel
    
        'Start Excel
        If Err.Number <> 0 Then    'Could not get instance of Excel, so create a new one
            Err.Clear
            On Error GoTo Error_Handler
            Set oExcel = CreateObject("Excel.Application")
            bExcelOpened = False
        Else    'Excel was already running
            bExcelOpened = True
        End If
        On Error GoTo Error_Handler
        oExcel.ScreenUpdating = False
        oExcel.Visible = False   'Keep Excel hidden until we are done with our manipulation
        Set oExcelWrkBk = oExcel.Workbooks.Add()    'Start a new workbook
        Set oExcelWrSht = oExcelWrkBk.Sheets(1)
    
        'Open our SQL Statement, Table, Query
        Set db = CurrentDb
        Set rs = db.OpenRecordset(sQuery, dbOpenSnapshot)
        With rs
              If .RecordCount <> 0 Then
                'Build our Header
                For iCols = 0 To rs.Fields.Count - 1
                    oExcelWrSht.Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
                Next
                With oExcelWrSht.Range(oExcelWrSht.Cells(1, 1), _
                                       oExcelWrSht.Cells(1, rs.Fields.Count))
                    .Font.Bold = True
                    .Font.ColorIndex = 2
                    .Interior.ColorIndex = 1
                    .HorizontalAlignment = xlCenter
                End With
                'Copy the data from our query into Excel
                oExcelWrSht.Range("A2").CopyFromRecordset rs 'Resize columns based on the headings and data fields
                                oExcelWrSht.Cells.EntireColumn.AutoFit
                                oExcelWrSht.Cells.EntireRow.AutoFit
                oExcelWrSht.Range("A1").Select  'Return to the top of the page
            Else
                MsgBox "There are no records returned by the specified queries/SQL statement.", vbCritical + vbOKOnly, "No data to generate an Excel spreadsheet with"
                GoTo Error_Handler_Exit
            End If
        End With
    
    Error_Handler_Exit:
        On Error Resume Next
        oExcel.Visible = True   'Make excel visible to the user
        rs.Close
        Set rs = Nothing
        Set db = Nothing
        Set oExcelWrSht = Nothing
        Set oExcelWrkBk = Nothing
        oExcel.ScreenUpdating = True
        Set oExcel = Nothing
        Exit Function
    
    Error_Handler:
        MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
               "Error Number: " & Err.Number & vbCrLf & _
               "Error Source: Export2XLS" & vbCrLf & _
               "Error Description: " & Err.Description _
               , vbOKOnly + vbCritical, "An Error has Occurred!"
        Resume Error_Handler_Exit
    End Function


    Suggestions?

  2. #2
    mlrucci is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Apr 2018
    Posts
    202
    Update, so I was working on trying to fix the issue, I realized that I have the sql in vba that is different than the qry I am asking access to export to excel. Guess the question is going to be, how can I get the all the records including those that have false ck?

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    TBH, I am not sure why you would need the Eval()?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    mlrucci is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Apr 2018
    Posts
    202
    I don’t disagree, but that is the only way the export code works. Not sure why, but don’t know how else to make it work.

  5. #5
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    If you remove the IsComponentFieldRequired criteria completely but display the field do you get True and False where you would expect to?

    Is it a calculated field?
    Are there Null values?

    Is this an SQL Server linked table?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    how can I get the all the records including those that have false ck
    Remove the criteria/constraint? Is that this part?
    Code:
    AND ((lnkComponentToMCField.IsComponentFieldRequired)=True Or (lnkComponentToMCField.IsComponentFieldRequired)=False))


    I could see the need for eval if you were trying to get a value from a combo column but not with that. Maybe it has something to do with the nested nav forms.

    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    mlrucci is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Apr 2018
    Posts
    202
    Micron, after you remark, I went back and looked at my original query. I have a table with a relationship to another table. I forgot to change the relationship to Left to Right. That was my bad. Appreciate the help. Now is works. It wasn't just the Is ComponentFieldRequired. Just didn't see the other fields that weren't included due to the relationship I set up.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 09-01-2022, 04:19 PM
  2. Export to Excel - get Repaired Records: Error
    By dwaterman in forum Queries
    Replies: 3
    Last Post: 01-01-2017, 01:50 PM
  3. Export the Access selected Records to Excel
    By Nadal in forum Programming
    Replies: 5
    Last Post: 06-01-2016, 02:30 AM
  4. Export To Excel Through Macro is Duplicating Records
    By raykdogs in forum Import/Export Data
    Replies: 2
    Last Post: 05-05-2015, 04:30 AM
  5. Replies: 3
    Last Post: 03-16-2014, 08:09 PM

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