How to reuse code? (1 Viewer)

Everterra

Registered User.
Local time
Today, 15:38
Joined
Jan 12, 2015
Messages
16
Please help to correct this code to reuse it on clicking a button.

Code:
Function EmiteRecibo()
On Error GoTo EmiteRecibo_Err

    Dim strArquivo As String
    Dim strLocal As String

    strArquivo = "Recibo" & Me!RecTId & Me!TbEntAbv & ".pdf"
    'aponta para a pasta do ficheiro pdf a criar onde está a bd
    strLocal = CurrentProject.Path & "\recibos\" & strArquivo
    
    DoCmd.Close acForm, "ReciboComposiçãoF"
    DoCmd.OpenQuery "1RecibosEmissaoQ", acViewNormal, acReadOnly
    DoCmd.OpenQuery "2RecibosEmissaoQ", acViewNormal, acReadOnly
    DoCmd.OpenReport "ReciboRelatório", acViewPreview, "", "[RecTId]=DLast(""RecTId"",""RecibosT"")", acHidden
    Beep
    MsgBox "O recibo foi emitido.", vbInformation, "Emissão de recibos"
    
    DoCmd.OutputTo acOutputReport, "ReciboRelatório", acFormatPDF, strLocal
        
    DoCmd.OpenReport "ReciboRelatório", acViewPreview, "", "[RecTId]=DLast(""RecTId"",""RecibosT"")", acHidden
    
EmiteRecibo_Exit:
    Exit Function

EmiteRecibo_Err:
    MsgBox Error$
    Resume abre_Exit

End Function


What I must correct to use this code in this, when clicking a button?:

Code:
Private Sub Comando285_Click()

End Sub

Much obliged!
 

darbid

Registered User.
Local time
Today, 16:38
Joined
Jun 26, 2008
Messages
1,428
If I understand your question then the answer is this;

Code:
Private Sub Comando285_Click()
    Call  EmiteRecibo
End Sub
This assumes that for example you have done some error checking, eg. Me!RecTId & Me!TbEntAbv actually contain values.
 

Everterra

Registered User.
Local time
Today, 15:38
Joined
Jan 12, 2015
Messages
16
If I understand your question then the answer is this;

Code:
Private Sub Comando285_Click()
    Call  EmiteRecibo
End Sub
This assumes that for example you have done some error checking, eg. Me!RecTId & Me!TbEntAbv actually contain values.

Thanks darbi.

This give me the error:
"Expected variable or procedure, not module.":banghead:

I have the code in a module converted from a macro.

This assumes that for example you have done some error checking, eg. Me!RecTId & Me!TbEntAbv actually contain values

That´s an error too. I was waiting to solve the first error.

The code run ok until the end, but not produce a pdf file as expected.

Initially the code was used in a form and carrying the value of that form, but now I want to use it in a report to open and a produce a pdf file of the last record.

The report opens correctly, but no files are generated!

The line of code must be shifted ...?

The report have "RecTId" (this is the number of the report) and "TbEntAbv" is hidden.
 
Last edited:

sneuberg

AWF VIP
Local time
Today, 07:38
Joined
Oct 17, 2014
Messages
3,506
If the function references a form as it does with the Me reference it needs to be in the same form module that has the controls RecTId and TbEntAbv. The Me is translated to the form name. If you want to reuse this in different forms then you could put it in a standard module and pass values of RecTId and TbEntAbvthe as parameters. You could change the function like:

Code:
Function EmiteRecibo(RecTId As Variant, TbEntAbv As Variant)
On Error GoTo EmiteRecibo_Err

    Dim strArquivo As String
    Dim strLocal As String

    strArquivo = "Recibo" & RecTId & TbEntAbv & ".pdf"
    'aponta para a pasta do ficheiro pdf a criar onde está a bd
    strLocal = CurrentProject.Path & "\recibos" & strArquivo
    
    DoCmd.Close acForm, "ReciboComposiçãoF"
    DoCmd.OpenQuery "1RecibosEmissaoQ", acViewNormal, acReadOnly
    DoCmd.OpenQuery "2RecibosEmissaoQ", acViewNormal, acReadOnly
    DoCmd.OpenReport "ReciboRelatório", acViewPreview, "", "[RecTId]=DLast(""RecTId"",""RecibosT"")", acHidden
    Beep
    MsgBox "O recibo foi emitido.", vbInformation, "Emissão de recibos"
    
    DoCmd.OutputTo acOutputReport, "ReciboRelatório", acFormatPDF, strLocal
        
    DoCmd.OpenReport "ReciboRelatório", acViewPreview, "", "[RecTId]=DLast(""RecTId"",""RecibosT"")", acHidden
    
EmiteRecibo_Exit:
    Exit Function

EmiteRecibo_Err:
    MsgBox Error$
    Resume [COLOR="Blue"]EmiteRecibo_Exit[/COLOR]

End Function

And for example call it like

Code:
Call EmiteRecibo(Me!RecTId, Me!TbEntAbv)

I changed abre_Exit to EmiteRecibo_Exit in the function as abre_Exit doesn't exist in the code.

This give me the error:
"Expected variable or procedure, not module."

I suspect your module name is the same as the procedure name. It's alway best to make sure they are different. If nothing else just suffix them with module, e.g., EmiteRecibo Module
 
Last edited:

darbid

Registered User.
Local time
Today, 16:38
Joined
Jun 26, 2008
Messages
1,428
I have the code in a module converted from a macro.
Put "Public" in front of Function. Also please read up on this topic which will help you understand why it failed in the first place http://www.cpearson.com/excel/scope.aspx

But if you put this in a module then I will give you a hint, you cannot use Me! which is also an issue with scope.

That´s an error too. The code run ok until the end, but not pruduce a pdf file as expected.
put a break point on
Code:
 strArquivo = "Recibo" & Me!RecTId & Me!TbEntAbv & ".pdf"
and then start your function. Check that strArquivo becomes what you expect it to and then what strLocal is.
 

Everterra

Registered User.
Local time
Today, 15:38
Joined
Jan 12, 2015
Messages
16
If the function references a form as it does with the Me reference ...

Sorry, meanwhile I edited the last post.

Now I want to apply the code to a report that opens the last record.

As I said in the edited post:

"The code runs ok until the end, but not produce a pdf file as expected.

Initially the code was used in a form and carrying the value of that form, but now I want to use it in a report to open and a produce a pdf file of the last record.

The report opens correctly, but no files are generated!

The line of code must be shifted ...?

The report have "RecTId" (this is the number of the report) and "TbEntAbv" is hidden."


While I do not see the solution to the output of the file I will try to correct it with the information you gave me.
 

Everterra

Registered User.
Local time
Today, 15:38
Joined
Jan 12, 2015
Messages
16
I made it.

I put the code inside the form and call the function on the click of the button.

Thanks for the help.
 

Users who are viewing this thread

Top Bottom