通过 MS Access 导航表单对“另存为”按钮进行故障排除

发布于 2025-01-17 01:12:00 字数 984 浏览 2 评论 0原文

我一直在努力让我的 VBA 工作起来&我很茫然,因为我对 VBA 或一般编码非常陌生。

我所拥有的基本上是一个导航主表单,它使用选项卡打开不同的表单以方便访问。在这些子表单之一上有一个按钮,应该用作“SaveAsPDF”选项。它基本上应该通过打开一个你想要保存它的文件夹来工作,&将报告版本以 pdf 格式导出到该位置。奇怪的是,当您打开实际的表单并打开它时,它可以完美地工作。不是在导航菜单中打开的表单,所以我现在不知所措。

如果有人能够提供帮助,我们将不胜感激。你会节省很多地板上的头发。我得到的是下面的内容,


Private Sub SaveAsPDF_Click()
Dim fd As FileDialog
On Error goto ErrorHandler

Set fd = Application.FileDialog(msoFileDialogSaveAs)
    With fd
        .InitialFileName = MAFNO & ".pdf"

    End With

If fd.Show Then
    DoCmd.OutputTo acOutputReport, "RptMAFPrint", acFormatPDF, _
    fd.SelectedItems(1), True
End If

Exit sub

ErrorHandler:
Msgbox "An Error occurred, please try again", vbinformation, "Could not save document"
Exit sub

End Sub

就当您打开实际表单时它自己运行而言,它工作得很好并且可以正常工作。没有出现任何错误,但是当从导航菜单选项卡打开它时,它立即出现错误并显示错误。尝试保存整个工作簿。 以下错误 https://i.sstatic.net/KqIEq.jpg

i've been struggling around trying to get my VBA to work & i'm at a loss since i'm extremely fresh using VBA or coding in-general.

What i've got is basically a Navigation Main Form that uses tabs to open up different forms for ease of access. On one of these subforms there is a button that is supposed to work as a "SaveAsPDF" option. it's basically supposed to work by opening up a folder you want to save it in, & exporting the Report version as a pdf to the location. The weird thing is that it works perfectly when you have the actual form open & not the form open in the navigation menu, so i'm at a loss now.

If anyone is able to help, it's much appreciated & you'll be saving alot of hair from the floor. What i've got is below


Private Sub SaveAsPDF_Click()
Dim fd As FileDialog
On Error goto ErrorHandler

Set fd = Application.FileDialog(msoFileDialogSaveAs)
    With fd
        .InitialFileName = MAFNO & ".pdf"

    End With

If fd.Show Then
    DoCmd.OutputTo acOutputReport, "RptMAFPrint", acFormatPDF, _
    fd.SelectedItems(1), True
End If

Exit sub

ErrorHandler:
Msgbox "An Error occurred, please try again", vbinformation, "Could not save document"
Exit sub

End Sub

As far as it running on it's own when you open the actual form up, it works perfectly & hasn't had a single error, but when opening it up from the Navigation Menu Tab it immediately has an error & tries to save the entire workbook.
Error below
https://i.sstatic.net/KqIEq.jpg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不回头走下去 2025-01-24 01:12:00

这篇文章已经过去很久了,但科斯塔斯已经给出了答案&我可以轻松地编辑 vba 使其工作。谢谢你,谢谢科斯塔斯,如果我真的能把你的作为答案,我会的。

如果有人需要的话,VBA 就在下面。我可能会在某个时候回去看看我是否可以更好地重写它,但它可以完成大部分工作。
这对于 MS Access 导航表单来说确实非常有效。它不会在 VBA 末尾关闭当前活动选项卡。

Private Sub ButtonSaveAsPDF_Click()
Dim fd As FileDialog
On Error GoTo ErrorHandler

DoCmd.Save
DoCmd.RefreshRecord

'====  Initial Check  ====

If IsNull(Me.TxtMAFNumberTop) Then
    GoTo Leave

ElseIf Not IsNull(Me.TxtMAFNumberTop) Then
    GoTo Start

End If

'====  SaveAs PDF Start  ====
Start:
    Set fd = Application.FileDialog(msoFileDialogSaveAs)
        With fd
        .InitialFileName = TxtMAFNumberTop & ".pdf"
    
        End With

    If fd.Show Then
        DoCmd.OpenForm "FrmMAF", acNormal, , "[ID] = " & ID, , acHidden
        DoCmd.OutputTo acOutputReport, "RptMAFPrint", acFormatPDF, _
        fd.SelectedItems(1), True
        DoCmd.Close , "FrmMAF"

    End If

Exit Sub

Leave:
Exit Sub

ErrorHandler:
    MsgBox "An error occurred trying to print your MAF. Please try the following 
below & try again" & vbCrLf & _
    vbCrLf & _
    "1) Verify current MAF information & fields are correct" & vbCrLf & _
    "2) Cycle Records & try again " & vbCrLf & _
    vbCrLf & _
    "If this problem occurs again please contact your local Administrator", _
    vbCritical + vbRetryCancel, _
    "[Critical] ERROR : SaveAsPDF "

    If vbRetry Then
        GoTo Start
    
    ElseIf vbCancel Then
        Exit Sub
    
    End If

End Sub

Longpast on this post, but Kostas had the answer & I was easily able to edit the vba to make it work. Thank you thank you kostas, if I could actually put yours as the answer, I would.

VBA is below if anyone would ever need it. I'll probably go back at some point & see if I could re-write it better, but it gets the job done for the most part.
This does work perfectly for a MS Access Navigation form though & it does not close the current active tab at the end of the VBA.

Private Sub ButtonSaveAsPDF_Click()
Dim fd As FileDialog
On Error GoTo ErrorHandler

DoCmd.Save
DoCmd.RefreshRecord

'====  Initial Check  ====

If IsNull(Me.TxtMAFNumberTop) Then
    GoTo Leave

ElseIf Not IsNull(Me.TxtMAFNumberTop) Then
    GoTo Start

End If

'====  SaveAs PDF Start  ====
Start:
    Set fd = Application.FileDialog(msoFileDialogSaveAs)
        With fd
        .InitialFileName = TxtMAFNumberTop & ".pdf"
    
        End With

    If fd.Show Then
        DoCmd.OpenForm "FrmMAF", acNormal, , "[ID] = " & ID, , acHidden
        DoCmd.OutputTo acOutputReport, "RptMAFPrint", acFormatPDF, _
        fd.SelectedItems(1), True
        DoCmd.Close , "FrmMAF"

    End If

Exit Sub

Leave:
Exit Sub

ErrorHandler:
    MsgBox "An error occurred trying to print your MAF. Please try the following 
below & try again" & vbCrLf & _
    vbCrLf & _
    "1) Verify current MAF information & fields are correct" & vbCrLf & _
    "2) Cycle Records & try again " & vbCrLf & _
    vbCrLf & _
    "If this problem occurs again please contact your local Administrator", _
    vbCritical + vbRetryCancel, _
    "[Critical] ERROR : SaveAsPDF "

    If vbRetry Then
        GoTo Start
    
    ElseIf vbCancel Then
        Exit Sub
    
    End If

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文