Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Continuous Form - Check Box Do? 2

Status
Not open for further replies.

misscrf

Technical User
Jun 7, 2004
1,344
US
My code is messy, because I have been trying to understand how to use recordset.clone. I have a continuous form. There is a filter in the header, which filters a partial string to a form control in the detail. It limits all "assets" in a project to a project id, the project ID being the prefix of all suffixes.

I also have a "temp" checkbox in the table, so that as assets are created, a user can check multiple assets, and generate custom metadata for each of them.

So this is what I want to do. Check which ever checkboxes I need. Click a command button in the header. The code will grab the current records in the filtered (or not filtered) form, and then for each record that has a checked checkbox, it will run a union query filtered to that record's asset/batch name, and save it as a csv to the specified directory, using the batch name as the file name.

Then it will move to the next, and so on, until all checked batches have a csv made. Once done, it will clear all checkboxes.

Make sense? I know my code is wrong, I just don't know what I am doing, and am having the hardest time searching this. I have been searching recordset, continuous forms, filters, checkboxes, etc.

Here is the code I have been working back and forth with. It is including msgbox's because I am trying to figure out what information the code is actually getting...

Code:
Private Sub cmdGenerateProjectMetadata_Click()
On Error GoTo Err_cmdGenerateProjectMetadata_Click

Dim frm As Form
Dim strTarget As String
Dim strDoc As String
Dim strFileName As String
Dim strPath As String
Dim strExportFile As String
Dim rs As DAO.Recordset
'Dim strSql As String
Dim strSql2 As String
'Set frm = Forms!frmProcessingTracking
'Set rs = frm.RecordsetClone
strSql2 = Forms!frmProcessingTracking.RecordSource
'& " Where " & Me.Filter
Me.RecordSource = strSql2
'Me.FilterOn = True
MsgBox Me.RecordSource
strPath = InputBox("Please Enter a UNC Path for the Custom Metadata files to be saved to", _
"Project Files Custom Metadata Location") & "\"
If strPath = "" Then
    MsgBox "Must set path for metadata to generate to", vbOKOnly, "Custom Metadata Must Be Generated to a Folder"
    DoCmd.CancelEvent
Else
    rs.MoveFirst
    MsgBox (rs.RecordCount)
        For i = 1 To rs.RecordCount
             If Me.chkGen = 1 Then
                MsgBox ("IN THE LOOP")
                strDoc = "qryUnionCustomMetadata"
                strFileName = Forms![frmProcessingTracking].Form![ProjectEvidenceFileBatch]
                strExportFile = strPath & strFileName & ".csv"
                DoCmd.TransferText acExportDelim, strDoc, strExportFile, False
            'ProjectMetadataExport
        End If
        rs.MoveNext
    Next i
End If
rs.Close
Set rs = Nothing
DoCmd.SetWarnings False
strSql = "UPDATE tblDiscoveryProcessing SET GenerateMetadatatmp = 0;"
DoCmd.RunSQL strSql
Me.Requery 'show changes in form
DoCmd.SetWarnings True
MsgBox "All custom Metadata has been generated", vbOKOnly, "Custom Metadata Generated"
Exit_cmdGenerateProjectMetadata_Click:
    Exit Sub

Err_cmdGenerateProjectMetadata_Click:
    MsgBox Err.Description
    Resume Exit_cmdGenerateProjectMetadata_Click
End Sub

Any help would be greatly appreciated!

misscrf

It is never too late to become what you could have been ~ George Eliot
 
A checkbox in a continuous form must be bound to a table. Changing any unbound control will change all of them. Your form has to have somewhere to save the Value of the checkbox for each record. Now getting to your code your not kidding ...(but we all had to learn somewhere :) ) try Debug.print for code debugging.

Just a couple of glaring things re your code

With recordsets you need to set the db as follows

Dim db as DAO.Database
Dim rs as DAO.Recordset

Set db = Currentdb()
Set rs = db.OpenRecordSet("rsNamehere")


Also replace DoCmd.CancelEvent with simply 'Exit Sub'

When doing a rs.RecordCount its a good habit to make sure you populate the rs with all records with

rs.MoveLast
rs.MoveFirst

I would recommend to anyone who doesn't have it or is just getting started an access addin called MZTools and another utility called SmartIndenter just google them I dont have an Access install without them

Thats for starters :) Thoughts?

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
Thank you for responding. The checkbox is a bound to a field in at table. The field is called GenerateMetadatatmp

You can see it at the bottom of the code where I run the update statement to reset any checked items in the table, back to 0 (unchecked).

When you say "rsNamehere" what can I set that as?

I tried this:
Code:
Dim rst As DAO.Recordset
    Set rst = Application.CurrentDb.OpenRecordset("qryDiscoveryProcessingCustomChecked", dbOpenDynaset)
MsgBox rst.Fields(0)

That did not work. I get a type mismatch in expression.

What I did, was make a query that has 2 columns - the batch name that I need as my criteria for my union query (the one that transposes data and needs to go into a csv) and then the checkbox, which a criteria that it = -1


I know that what I want to do, should be able to be done. But I can't seem to get the syntax right.

Thanks for your help!

misscrf

It is never too late to become what you could have been ~ George Eliot
 
So i think i understand what you want you want to loop through the 'form' and update if true? in other words the underlying table the form is bound to ? so ... forget about all of your code and update based on a query of the underlying table assuming your form is updating the table.


Dim db as DAO.Database
Dim rs as DAO.RecordSet

Set db = Currentdb()
Set rs = db.OpenRecordSet("formsRecordSetMatchingTrue") << query of the underlying table

Now run your loop to send your reports
When done reset the values to false with vba


Thoughts?

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
I may need to try a different way to explain. The form has a record source to a table called tblProcessing. This is the processing of each batch. One field is the Asset. That is linked to the Asset tracking table tblAssetTracking.

Now I have a combo box in the header of this processing form, to filter the assets. What this does is takes the first 6 characters of the assets, and filters to all assets with those first 6 characters, which have any processing records. The first 6 characters are the project ID. So 1 project will have many assets. Each asset will have many processing batches/records.

Whenever I set up a new record for processing, I have to run a query of data. This query gives me information about the asset and the processing - date asset was collected, which batch this is for the asset (one asset could have 20 processing batches), the date processed, etc.

Now the query I need to run, must be run for 1 record at a time. It is a non-traditional union query. Here is the syntax:

Code:
SELECT "Name" AS ColName,"Value" AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "AssetTag" AS ColName, qryAssetBatchProcessingMetadata.AssetTag AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "CollectedFrom" AS ColName, qryAssetBatchProcessingMetadata.CollectedFrom AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "CollectedLocation" AS ColName, qryAssetBatchProcessingMetadata.CollectedLocation AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "HDDCopyToSN" AS ColName, qryAssetBatchProcessingMetadata.HDDCopyToSN AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "Custodian" AS ColName, qryAssetBatchProcessingMetadata.Custodian AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "DateCollected" AS ColName, qryAssetBatchProcessingMetadata.DateCollected AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "DateProcessed" AS ColName, qryAssetBatchProcessingMetadata.DateProcessed AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "ProcessingDatabase" AS ColName, qryAssetBatchProcessingMetadata.NuixCase AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "ProcessingBatchName" AS ColName, qryAssetBatchProcessingMetadata.NuixEvidenceFileBatch AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "OriginalHDDSN" AS ColName, qryAssetBatchProcessingMetadata.HDDOrigSN AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "SourceDescription" AS ColName, qryAssetBatchProcessingMetadata.SourceDescription AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL 
SELECT "AssetNotes" AS ColName, qryAssetBatchProcessingMetadata.AssetNotes AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL 
SELECT "DirectoriesProcessed" AS ColName, qryAssetBatchProcessingMetadata.DirectoriesProcessed AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL 
SELECT "ProcessingNotes" AS ColName, qryAssetBatchProcessingMetadata.ProcessingNotes AS ColValue
FROM qryAssetBatchProcessingMetadata;

So we have an order:
[ul]
[li]Enter an asset into the frmAssetTracking (separate form, just showing you workflow)[/li]
[li]Open the frmProcessingTracking[/li]
[li]Filter to all processing records within one project[/li]
[li]Fill out new processing batches, linked to assets[/li]
[li]Check boxes for ones that need custom metadata[/li]
[li]Click a command button (where I need our code to do the magic that comes next)[/li]
[li]For each checked box, run the union query, filtered to that batch name, and export to a csv, at the provided directory. Do the next one and the next one til done[/li]
[li]Uncheck all checked boxes.[/li]
[/ul]

The checkbox is a field in the table, but one I only created for this very purpose. We are working with continuous forms, as we need to look at all the rows for a given project at a time.

Does that explain things better? I think sometimes my bad explanation is just an indicator of my confusion at the task I am trying to solve lol.

Thanks!


misscrf

It is never too late to become what you could have been ~ George Eliot
 
how about

Code:
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone

rs.MoveFirst

For i = 1 To rs.RecordCount
    If Me.CheckBoxName = True Then
    'Do Something...
    End If
    rs.MoveNext
Next i

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
Thanks for your help. I set it up with these specifics:

Code:
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone

MsgBox rs.RecordCount

rs.MoveFirst

For i = 1 To rs.RecordCount
        If Me.GenerateMetadatatmp.Value = True Then
            MsgBox Me.ProjectBatch
        End If
    rs.MoveNext

Next i
rs.Close

Set rs = Nothing

I get 5 for the first message box. The reality is that there are 5 records in the form - as I have a filter on the form, restricting the listed table records to one test project.

For the next msgbox (only if the check is true), I get the project batch name of one of the checked records on the form. But then I get that same one 4 more times, a total of 5, one for every record.

I think I need to do For i = 1 To rs.RecordCount after I do the if, and have the rs.recordcount where it's true. Then I also don't know why I am not getting the other ones, but I think that is a big part of the problem.

This is at least running without error!




misscrf

It is never too late to become what you could have been ~ George Eliot
 
Replace this:
Set rs = Me.RecordsetClone
with this:
Set rs = Me.Recordset

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks, PHV! I got really close. I was able to list each one, but now I am at the issue of filtering this union query.

Here is the code I have, all put together:
Code:
Private Sub cmdGenerateProjectMetadata_Click()
On Error GoTo Err_cmdGenerateProjectMetadata_Click

Dim strDoc As String
Dim strFileName As String
Dim strPath As String
Dim strExportFile As String
Dim rs As DAO.Recordset
Set rs = Me.Recordset

MsgBox rs.RecordCount
strPath = InputBox("Please Enter a UNC Path for the Custom Metadata files to be saved to", _
"Project Files Custom Metadata Location") & "\"
If strPath = "" Then
    MsgBox "Must set path for metadata to generate to", vbOKOnly, "Custom Metadata Must Be Generated to a Folder"
    DoCmd.CancelEvent
Else
rs.MoveFirst
For i = 1 To rs.RecordCount
        If Me.GenerateMetadatatmp.Value = True Then
            MsgBox Me.ProjectEvidenceFileBatch
                strDoc = "qryUnionCustomMetadata"
                strFileName = Forms![frmProcessingTracking].Form![ProcessingBatchName]
                strExportFile = strPath & strFileName & ".csv"
                DoCmd.TransferText acExportDelim, strDoc, strExportFile, False
        End If
    rs.MoveNext

Next i
End If
rs.Close
Set rs = Nothing
DoCmd.SetWarnings False
strSql = "UPDATE tblDiscoveryProcessing SET GenerateMetadatatmp = 0;"
DoCmd.RunSQL strSql
Me.Requery
DoCmd.SetWarnings True
MsgBox "All custom Metadata has been generated", vbOKOnly, "Custom Metadata Generated"
   
Exit_cmdGenerateProjectMetadata_Click:
    Exit Sub
Err_cmdGenerateProjectMetadata_Click:
    MsgBox Err.Description
    Resume Exit_cmdGenerateProjectMetadata_Click
End Sub

I get the 5 messagebox for the rs.record count. Then I get the first batch name. Then I get this error message:
---------------------------
Microsoft Access
---------------------------
The text file specification 'qryUnionCustomMetadata' does not exist. You cannot import, export, or link using the specification.
---------------------------
OK
---------------------------

Here is the SQL of that query again (so you don't have to scroll up and find it)
Code:
SELECT "Name" AS ColName,"Value" AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "AssetTag" AS ColName, qryAssetBatchProcessingMetadata.AssetTag AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "CollectedFrom" AS ColName, qryAssetBatchProcessingMetadata.CollectedFrom AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "CollectedLocation" AS ColName, qryAssetBatchProcessingMetadata.CollectedLocation AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "HDDCopyToSN" AS ColName, qryAssetBatchProcessingMetadata.HDDCopyToSN AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "Custodian" AS ColName, qryAssetBatchProcessingMetadata.Custodian AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "DateCollected" AS ColName, qryAssetBatchProcessingMetadata.DateCollected AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "DateProcessed" AS ColName, qryAssetBatchProcessingMetadata.DateProcessed AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "ProcessingDatabase" AS ColName, qryAssetBatchProcessingMetadata.NuixCase AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "ProcessingBatchName" AS ColName, qryAssetBatchProcessingMetadata.NuixEvidenceFileBatch AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "OriginalHDDSN" AS ColName, qryAssetBatchProcessingMetadata.HDDOrigSN AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL
SELECT "SourceDescription" AS ColName, qryAssetBatchProcessingMetadata.SourceDescription AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL 
SELECT "AssetNotes" AS ColName, qryAssetBatchProcessingMetadata.AssetNotes AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL 
SELECT "DirectoriesProcessed" AS ColName, qryAssetBatchProcessingMetadata.DirectoriesProcessed AS ColValue
FROM qryAssetBatchProcessingMetadata
UNION ALL 
SELECT "ProcessingNotes" AS ColName, qryAssetBatchProcessingMetadata.ProcessingNotes AS ColValue
FROM qryAssetBatchProcessingMetadata;

What this does is transpose the information I need, so that it is saved to the csv the right way.

Instead of a normal query with
Column1Name Column2Name Column3Name
Column1Value Column2Value Column3Value

I need
Name Value
ColumnName1 Column1Value
ColumnName2 Column2Value
ColumnName3 Column3Value

The union does that. I swear I got it to filter at one point in time, but now it won't. Any thoughts?

That filter would be on the rs field for ProcessingBatchName = the ProcessingBatchName on the form, which I am getting in the msgbox test.

Thanks!



misscrf

It is never too late to become what you could have been ~ George Eliot
 
I got it!!!! Only 1 problem, and then I am set!

Here is the code that works (99%)

Code:
Private Sub cmdGenerateProjectMetadata_Click()
On Error GoTo Err_cmdGenerateProjectMetadata_Click

Dim strDoc As String
Dim strFileName As String
Dim strPath As String
Dim strExportFile As String
Dim rs As DAO.Recordset
Set rs = Me.Recordset

MsgBox rs.RecordCount
strPath = InputBox("Please Enter a UNC Path for the Custom Metadata files to be saved to", _
"Project Files Custom Metadata Location") & "\"
If strPath = "" Then
    MsgBox "Must set path for metadata to generate to", vbOKOnly, "Custom Metadata Must Be Generated to a Folder"
    DoCmd.CancelEvent
Else
rs.MoveFirst
For i = 1 To rs.RecordCount
        If Me.GenerateMetadatatmp.Value = True Then
            MsgBox Me.ProjectEvidenceFileBatch
                strDoc = "qryUnionCustomMetadata"
                strFileName = Forms![frmProcessingTracking].Form![ProjectBatchName]
                strExportFile = strPath & strFileName & ".csv"
                DoCmd.TransferText acExportDelim, , strDoc, strExportFile, False
        End If
    rs.MoveNext

Next i
End If
rs.Close
Set rs = Nothing
DoCmd.SetWarnings False
strSql = "UPDATE tblDiscoveryProcessing SET GenerateMetadatatmp = 0;"
DoCmd.RunSQL strSql
Me.Requery
DoCmd.SetWarnings True
MsgBox "All custom Metadata has been generated", vbOKOnly, "Custom Metadata Generated"
   
Exit_cmdGenerateProjectMetadata_Click:
    Exit Sub
Err_cmdGenerateProjectMetadata_Click:
    MsgBox Err.Description
    Resume Exit_cmdGenerateProjectMetadata_Click
End Sub

In the union query, for the union that has the batch name, I have this:
Code:
SELECT "ProcessingBatch" AS ColName, qryProjectMetadata.ProcessingBatch AS ColValue
FROM qryProjectMetadata
WHERE ProcessingBatch= Forms![frmProcessingTracking].Form![ProcessingBatch]

It outputs them all perfectly!!!!

My only issue is that after it is finished running, my form gets all yucky. That is the technical term. hahaha. It takes the continuous form, which has 5 records showing (at the time), leaves 1 record with #name in the value for every control. So weird! Even if I hit clear filters button on my form, it doesn't go away. I have to close and reload the form.

Any idea why the form gets yucky like that?

Thanks, so much!!!!

misscrf

It is never too late to become what you could have been ~ George Eliot
 
I think I got it. Because I still need the form to have a recordset, I can't actually close it and set it to nothing. I have to take out
rs.Close
Set rs = Nothing

Please let me know if that is bad. I had read a lot about it being bad practice to leave a recordset open, but that may not be the case when you are dealing with an open form's recordset, which you still want to use after you are done with the action you are using the recordset for.

hmmm. Did I make sense there? ;-)

misscrf

It is never too late to become what you could have been ~ George Eliot
 
Yep, never ever close the recordset of your form !

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
nice !! to see your problem solved ... another one for Tek Tips ;)

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top