Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 43
  1. #16
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    at the moment you are exporting 'Transalation2' - suggest you need to export 'tmpQry'

  2. #17
    ijaz8883 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    103
    Ok lets suppose I have created a query named QryTranslation
    Now its the code
    Private Sub saveFile_Click()
    vDir = "c:\temp"
    MakeDir vDir
    vFile = vDir & "Myfile.xls"
    Dim qdef As QueryDef
    Set qdef = CurrentDb.CreateQueryDef("QryTranslation", Me.RecordSource)
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "QryTranslation", vFile, True, tabname
    CurrentDb.QueryDefs.Delete qdef.Name
    End Sub
    Public Sub MakeDir(ByVal pvDir)
    Dim FSO
    On Error Resume Next
    Set FSO = CreateObject("Scripting.FileSystemObject")
    FSO.CreateFolder pvDir 'like MkDir pvDir
    Set FSO = Nothing
    End Sub

    But now its giving an error
    Click image for larger version. 

Name:	error.jpg 
Views:	30 
Size:	73.6 KB 
ID:	37055
    Now let me know what my code should be?


  3. #18
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    I’ve already told you, reread the posts

  4. #19
    ijaz8883 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    103
    at the moment you are exporting 'Transalation2' - suggest you need to export 'tmpQry'
    "tmpQry" query name or exact tmpQry???????
    I don't understand I am not expert in access statements!

  5. #20
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    Compare your code with what I provided - take note of all the differences, then add in the missing code

  6. #21
    ijaz8883 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    103
    Quote Originally Posted by Ajax View Post
    Compare your code with what I provided - take note of all the differences, then add in the missing code
    Now look at this error

    Click image for larger version. 

Name:	error.jpg 
Views:	21 
Size:	51.0 KB 
ID:	37130

  7. #22
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    looks to me like you still are not doing what I said.

    1. you create a query called 'tmpQry' - correct
    2. you export a query called 'qsDataExport' - incorrect
    3. you delete a query called 'tmpQry' - correct

    Next time you have a problem, help me to help you - I'm assuming the error is per 2 above

    a) copy paste the code, don't screenshot it
    b) by all means screenshot the error message, but please advise which line is causing the issue - click debug and the line will be highlighted

  8. #23
    ijaz8883 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    103
    Ok lets see
    I have One Table named "Translation2"
    I have One Query named "tmpQry"
    I have a Search form where data list is "tmpQry"
    Now its clear?
    My code to export the search data is:-
    Private Sub saveFile2_Click()
    Dim qdef As QueryDef
    Set qdef = CurrentDb.CreateQueryDef("tmpQry", Me.RecordSource)
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "tmpQry", vFile, True, tabname
    CurrentDb.QueryDefs.Delete qdef.Name
    End Sub
    The line where an error appeared via yellow highlighted:-
    Set qdef = CurrentDb.CreateQueryDef("tmpQry", Me.RecordSource)
    The error box says
    Run-time '3012'
    Object 'tempQry' already exists
    Thats it!!!Click image for larger version. 

Name:	error.jpg 
Views:	18 
Size:	80.0 KB 
ID:	37144

  9. #24
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    because you didn’t delete tmpqry per the initial code snippet it still exists - so you need to manually delete it I don’t know what you mean by datalist - if you mean the record source to you form that is wrong, recordsource should be your table

  10. #25
    ijaz8883 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    103
    Ok Thanks a lot
    I understand that you are unable to resolve the problem it was a simple search form where I want to export the filter data into excel file and you all never bother to try my provided database to correct it.
    Thanks to you all. I am still unsatisfied or unable to resolve my problem
    Now I shall try on other form to resolve my problem
    Good Day

  11. #26
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    Sorry you feel like that - however recommend on the other forum you provide a link to this thread so others do not provide the same suggestions

  12. #27
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Least said, soonest mended.

    ijaz8883,
    You have to delete the Querydef before create it.

    Try this code for the "saveexcel" button Click event and let us know if it works fine or not.
    Code:
    Private Sub saveexcel_Click()
        Dim qdf As dao.QueryDef
        Dim vDir As String
        Dim vFile As String
    
        vDir = "c:\temp\"
        MakeDir vDir
        vFile = "Myfile.xls"
    
        On Error Resume Next
        'Try to delete temporary querydef if exist
        CurrentDb.QueryDefs.Delete "QTemp"
    
        On Error GoTo ExitHere
        'Append the temporary querydef in QueryDefs collection
        Set qdf = CurrentDb.CreateQueryDef("QTemp", Me.RecordSource)
    
        'Create the Excel file
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, qdf.Name, vDir & vFile, True
        'There is no reason to delete the temporary querydef "QTemp" at this time.
        'It will the next time that will run this procedure.
        
        MsgBox "File '" & vFile & "' created successfully" _
               & vbCrLf & "in directory '" & vDir & "'" _
               , vbInformation
    ExitHere:
        If Err Then MsgBox "Error: " & Err & vbCrLf & Err.Description, vbExclamation
        'Release the qdf variable
        Set qdf = Nothing
    End Sub
    ...and, don't forget declare your variables. ;-)

    Regards,
    John

  13. #28
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    @accesstos - per post #23 'I have a Search form where data list is "tmpQry"' and mentioned in post #24, your delete query will not work because it is open in the form

  14. #29
    ijaz8883 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    103
    Quote Originally Posted by accesstos View Post
    Least said, soonest mended.

    ijaz8883,
    You have to delete the Querydef before create it.

    Try this code for the "saveexcel" button Click event and let us know if it works fine or not.
    Code:
    Private Sub saveexcel_Click()
        Dim qdf As dao.QueryDef
        Dim vDir As String
        Dim vFile As String
    
        vDir = "c:\temp\"
        MakeDir vDir
        vFile = "Myfile.xls"
    
        On Error Resume Next
        'Try to delete temporary querydef if exist
        CurrentDb.QueryDefs.Delete "QTemp"
    
        On Error GoTo ExitHere
        'Append the temporary querydef in QueryDefs collection
        Set qdf = CurrentDb.CreateQueryDef("QTemp", Me.RecordSource)
    
        'Create the Excel file
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, qdf.Name, vDir & vFile, True
        'There is no reason to delete the temporary querydef "QTemp" at this time.
        'It will the next time that will run this procedure.
        
        MsgBox "File '" & vFile & "' created successfully" _
               & vbCrLf & "in directory '" & vDir & "'" _
               , vbInformation
    ExitHere:
        If Err Then MsgBox "Error: " & Err & vbCrLf & Err.Description, vbExclamation
        'Release the qdf variable
        Set qdf = Nothing
    End Sub
    ...and, don't forget declare your variables. ;-)

    Regards,
    John

    Grate !!! Its work perfect as I desired.
    Thanks a lot
    Now I am satisfied

  15. #30
    ijaz8883 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    103
    Let me know can I use this statement in this code to create the file on desktop there would no need to create any temp folder/director

    Dim strFormName As String
    Dim strPathUser As String
    Dim strFilePath As String
    'set variables
    strFormName = "SearchResult"
    strPathUser = Environ$("USERPROFILE") & "\Desktop"
    strFilePath = strPathUser & strReportName & ".xls"

    This code will save the file on desktop but the code I have it does not export the file as your code but now I want to change the file save on desktop instead of C/Temp? is there any response?

Page 2 of 3 FirstFirst 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 02-25-2018, 09:35 AM
  2. Importing Data from Home Computer to Work Computer
    By wam18jr in forum Import/Export Data
    Replies: 5
    Last Post: 11-06-2017, 06:59 PM
  3. Replies: 4
    Last Post: 11-29-2012, 12:45 AM
  4. Replies: 2
    Last Post: 09-01-2011, 01:23 PM
  5. Replies: 2
    Last Post: 09-29-2009, 10:25 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