Code to export each query to separate csv BUT with certain conditions (1 Viewer)

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:12
Joined
May 7, 2009
Messages
19,230
same thing, first do the manual export to save the export spec.
then run the function to the remaining queires passing the same export spec.

I forgot to tell, what ever field you select select on manual import/export,
same field will be applied to the next import/export on diff csv.
 

jaryszek

Registered User.
Local time
Yesterday, 19:12
Joined
Aug 25, 2016
Messages
756
Ok thank you. I am still confused.

I created specCSV from one query.
And saved it manually. And now i can change path and save into another file with your function.

But how can i pass query which i want to export?
I have qery "qryTest" and specifiction is "specCSV" for it.
Now i want to export another query - "queryTest2" with the same spec.

How can i pass query into your function ?

Best,
Jacek
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:12
Joined
May 7, 2009
Messages
19,230
ok I revised the code to accept the query/table name:
Code:
Public Sub RunSpecOnOtherFile(ByVal SpecName As String, ByVal objName As String, _
                                    ByVal ObjType As String, _
                                    ByVal Path As String)
'
' arnelgp
'
' runs import/export specs to another file
'
' SpecName      = name of import/export spec
' objName       = name of query or table to import/export
' ObjType       = either "Query" or "Table" to import/export
' Path          = Path + filename(including extension) to create
'
    Dim objSpec As ImportExportSpecification
    Dim strXML As String
    Dim intStartPos As Integer, intEndPos As Integer
    
    Set objSpec = CurrentProject.ImportExportSpecifications.Item(SpecName)
    ' get the xml string
    strXML = objSpec.xml
    'Debug.Print
    'Debug.Print strXML
    'replace the Path part
    intStartPos = InStr(1, strXML, "Path")
    intStartPos = InStr(intStartPos, strXML, "=")
    intEndPos = InStr(intStartPos, strXML, "xmlns") - 1
    ' insert the new filename between intstartpos and intendpos
    strXML = Left(strXML, intStartPos) & " " & _
            """" & Path & """" & _
            Mid(strXML, intEndPos)
    'Debug.Print
    'Debug.Print strXML
    ' replace the objName
    strXML = RegExpReplace(strXML, "AccessObject=" & Chr(34) & "[\w]{1,}" & Chr(34), "AccessObject=" & Chr(34) & objName & Chr(34))
    'Debug.Print
    'Debug.Print strXML
    ' replace the ObjType
    strXML = RegExpReplace(strXML, "ObjectType=" & Chr(34) & "[\w]{1,}" & Chr(34), "ObjectType=" & Chr(34) & ObjType & Chr(34))
    'Debug.Print
    'Debug.Print strXML
    ' replace the xml of the spec to the new strXML
    objSpec.xml = strXML
    ' run the specification
    objSpec.Execute
    ' close it
    Set objSpec = Nothing
End Sub

''''''''''''
' helper
''''''''''''
Public Function RegExpReplace(ByVal WhichString As String, _
        ByVal Pattern As String, _
        ByVal ReplaceWith As String, _
        Optional ByVal IsGlobal As Boolean = True, _
        Optional ByVal IsCaseSensitive As Boolean = True) As String
    With CreateObject("vbscript.regexp")
        .Global = IsGlobal
        .Pattern = Pattern
        .IgnoreCase = Not IsCaseSensitive
        RegExpReplace = .Replace(WhichString, ReplaceWith)
    End With
End Function
 

sxschech

Registered User.
Local time
Yesterday, 19:12
Joined
Mar 2, 2010
Messages
792
Sounds like you have a solution. For my own understanding, curious about what the code I posted didn't provide. Only reason asking is that in reading the additional posts, seems that the other method requires a manual setup. The code I posted can directly export the data without having to manually create a spec first. Are there additional criteria/formatting issues not being addressed? Thanks.
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:12
Joined
Feb 19, 2002
Messages
43,257
There is nothing in the "old" spec that ties an export to a specific table or query. If you can post the database, we'll see if we can fix the problem.
I've attached a picture of the two spec tables and as you can see, neither includes anything regarding the name of the file being imported or exported where as the "new" import/Export does.


The "new" method DOES bind the spec to a specific file.


The code arnel has provided works with the "new" spec. For the "old" spec, you could manually update MSysIMEXSpecs and MSysIMEXColumns. Very few system tables allow direct update but these do.
 

Attachments

  • ExportSpec2.JPG
    ExportSpec2.JPG
    59.2 KB · Views: 171
  • ExportSpecNew.JPG
    ExportSpecNew.JPG
    40.9 KB · Views: 159

jaryszek

Registered User.
Local time
Yesterday, 19:12
Joined
Aug 25, 2016
Messages
756
thank you Guys very much.

sxschech - your method is ok but this is workaround with opening recordset - Access has native methods so i want to use them as much as i can. Looping through recordset can be slow.

Pat Hartman -o nice, good to know. i can not post database because of fragile data.
But i think i can handle with the new spec method and arnel code. I will test and let you know.
About old spec - there are specific columns there and what if another table which i want to export will have difference number of columns and columns types? It is to handle in old spec?

Best,
Jacek
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:12
Joined
Feb 19, 2002
Messages
43,257
Every distinct format needs a distinct spec. Import and Export specs can be used interchangeably as long as the format doesn't change. So if you import and export the same format, you can use the same spec.
 

jaryszek

Registered User.
Local time
Yesterday, 19:12
Joined
Aug 25, 2016
Messages
756
Ok thank you.

By
the format doesn't change
you mean fields format?
Like Number or String?

And most important, the number of fields? If i have spec where i am importing 10 fields and want to use it on table where i have 2 fields - will it work?

Best,
Jacek
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:12
Joined
Feb 19, 2002
Messages
43,257
The the field order is identical
the number of fields is identical
the format of each field is identical
the meaning of the field is identical (you don't want to switch client name for address even though they might have the same format)

Identical is identical. So NO, two columns in one file and 10 in the other are not identical specs.
 

jaryszek

Registered User.
Local time
Yesterday, 19:12
Joined
Aug 25, 2016
Messages
756
o wow.

So arneglgp code will do not work also.

Pat your method with old way will workaround this?
Or i have to use sxschech code ?

Jacek
 
Last edited:

jaryszek

Registered User.
Local time
Yesterday, 19:12
Joined
Aug 25, 2016
Messages
756
arnelgp so your code will only also on the same spec?

Best,
Jacek
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:12
Joined
May 7, 2009
Messages
19,230
yes, on same spec.
 

jaryszek

Registered User.
Local time
Yesterday, 19:12
Joined
Aug 25, 2016
Messages
756
Ok Guys so to sum up we have only 2 solutions:
1) Add manually for each exported query spec - and have it separately and use code to run them all, like "Spec_TableName" in the loop.
2) Use code to export recordset to external txt file.

Am i right?
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:12
Joined
Feb 19, 2002
Messages
43,257
You only have to create a spec manually ONCE. You save it (press the Advanced button to get to the save) and then you can reference it whenever you need it.

How many different formats do you have?
 

jaryszek

Registered User.
Local time
Yesterday, 19:12
Joined
Aug 25, 2016
Messages
756
Ok thank you Pat!!!

I have to only change field separator and decimal symbol but my columns are changing - i do not have the same number of columns in each table...

Best,
Jacek
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:12
Joined
Feb 19, 2002
Messages
43,257
You're welcome but that isn't the question I asked.
 

jaryszek

Registered User.
Local time
Yesterday, 19:12
Joined
Aug 25, 2016
Messages
756
Pat,

what do you mean by formats? I have about 15 queries with different number of columns and the same format (change decimal point and field separator only).

Best,
Jacek
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:12
Joined
Feb 19, 2002
Messages
43,257
As I said earlier, the files are not the same "format" unless EVERYTHING except the file name is identical. So, 15 specs is more than normal but not over the top. Just make the 15 specs and use the spec to control the file format.
 

jaryszek

Registered User.
Local time
Yesterday, 19:12
Joined
Aug 25, 2016
Messages
756
thank you Pat.

I will do this. But in case of more than 15 queries to export - only solution is to use exporting recordset to txt code - am i correct?

arnelgp code referring only to the situation that tables are the same with the same number of fields am i right?

Best,
Jacek
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:12
Joined
Feb 19, 2002
Messages
43,257
At some point, you might need to come up with a more general purpose solution involving code. Just keep in mind that to effectively use code, you will need to make your own version of the "Spec" dialog. You somehow have to predefine all the exports so your code can get the correct columns in the correct order in the correct format.

What are you trying to do? Would it be easier to export queries to spreadsheets? That has its own problems. If you don't need any formatting, you can just make queries. Here again, you have to make n queries. One for each export. So what are you doing and how infinite is the growth?
 

Users who are viewing this thread

Top Bottom