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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modify remote report using code 1

Status
Not open for further replies.

evalesthy

Programmer
Oct 27, 2000
513
US
Using the following code I can open a report in a 2nd database.

thread702-583654

By changing the docmd.open to acDesignView I can now open th remote report in design view. Is there a way to add code that would alter a label caption in that report and then save the change?
 
Yes, there is. After the DoCmd.OpenReport, add code like the following:
Code:
        .Reports(strReport).Controls(&quot;<label name>&quot;).Caption = &quot;New caption:&quot;

However, you might not want to use that code exactly as it is. It makes the remote database visible to the user, and waits for the user to close the database. It sounds to me as if you might want to do this without the user watching what's going on. Do you understand how that code works? If not, why don't you describe what you want from a step further back (such as &quot;I want to change the label on the report and then print it, without involving the user and without saving the change in the remote database&quot;, or &quot;I want to change the label and save it without previewing or printing the report&quot;).

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Thanks, yes, your assumption is correct. I would like to implement this change (and maybe others) without the user seeing the database open/close. It would be OK if all users needed to be out.

In short, I want to implement a change behind the scenes. User will see it when they next use that section of database (report in this instance). What is the best way to go about that.
 
Replace all the code with the following function. I enhanced it to allow you to change any property of any control.
Code:
Function ChangeRemoteReport(DbPath As String, _
                            ReportName As String, _
                            ControlName As String, _
                            PropertyName As String, _
                            NewValue As Variant) _
                            As Long
' Purpose: Changes any property of any control on any report in any external database
'          (assuming the property is read/write).
' Returns: 0 if successful, else runtime error number
Dim appAccess As Access.Application
Dim rpt As Report

    On Error GoTo ErrorHandler

    'If Len(Dir(DbPath)) > 0 Then
    Set appAccess = New Access.Application
    With appAccess
        .Echo False
        .OpenCurrentDatabase DbPath
        .DoCmd.OpenReport ReportName, acDesign
        Set rpt = .Reports(ReportName)
        rpt.Controls(ControlName).Properties(PropertyName) = NewValue
        Set rpt = Nothing
        .DoCmd.Save
        .DoCmd.Close acReport, ReportName
    End With
    ChangeRemoteReport = 0
    'End If
ErrorExit:
    On Error Resume Next
    appAccess.Quit
    Set appAccess = Nothing
    Exit Function
ErrorHandler:
    ChangeRemoteReport = Err.Number
    Select Case Err.Number
        Case 7866: ' Database not found or is already exclusively opened
            MsgBox &quot;The database you specified,&quot; & vbCrLf _
                & &quot;'&quot; & DbPath & &quot;',&quot; & vbCrLf _
                & &quot;was not found or is currently open in exclusive mode.&quot;, _
                vbExclamation, &quot;Could not open database&quot;
        Case 2103: ' Report doesn't exist
            MsgBox &quot;The report '&quot; & ReportName & &quot;' doesn't exist in the database &quot; _
                & vbCrLf & &quot;'&quot; & DbPath & &quot;'&quot;, _
                vbExclamation, &quot;Report not found&quot;
        Case 2465: ' Can't find the control
            MsgBox &quot;The control '&quot; & ControlName & &quot;' doesn't exist in the report '&quot; _
                & ReportName & &quot;'&quot;, _
                vbExclamation, &quot;Control not found&quot;
        Case 2455: ' Invalid reference to property
            MsgBox &quot;The control '&quot; & ControlName & &quot;' doesn't have a '&quot; _
                & PropertyName & &quot;' property&quot;, _
                vbExclamation, &quot;Property not found&quot;
        Case 2113: ' Invalid property value
            MsgBox &quot;The value '&quot; & NewValue & &quot;' is not valid for the '&quot; _
                & PropertyName & &quot;' property&quot;, _
                vbExclamation, &quot;Invalid property setting&quot;
        Case Else:
            MsgBox &quot;Error &quot; & Err.Number & &quot;:&quot; & vbCrLf & Err.Description, _
                    vbCritical + vbOKOnly, &quot;Runtime error&quot;
    End Select
    Resume ErrorExit
End Function
Note that the taskbar will briefly show an Access task running. I don't know how to prevent that, but it's only briefly there.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Excellent. It worked perfectly. Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top