Dont run if no Data (1 Viewer)

Gismo

Registered User.
Joined
Jun 12, 2017
Messages
1,298
Hi all,

Please could you advise on how to stop below code if no data

Private Sub Submit_Click()

Dim StrFileName As String
Dim StrSaveFile As String

StrFileName = strGetFileFolderName("Sales Invoice" & " - Customer -" & " " & Forms![Sales_Invoice]![CustomerName], 2, "Excel")

If Len(StrFileName & "") < 1 Then
StrFileName = Application.CurrentProject.Path & "\Sales Invoice" & Format(Date, "yyyymmdd") & ".xls"
End If

DoCmd.SetWarnings False
DoCmd.OpenQuery "Update Document Sequence - SI"
DoCmd.OpenQuery "Update Transactions- Commercial SI"
DoCmd.OpenQuery "Update Transactions - Commercial - Invoice Number"
DoCmd.OpenQuery "Update Payments from Invoice"

DoCmd.OutputTo acReport, ("Sales Invoice"), acFormatPDF, ("Sales Invoice" & " - Customer Name -" & " " & Forms![Sales_Invoice]![CustomerName] & ".PDF")

Submit_Click_Exit:


DoCmd.Close , ""
DoCmd.OpenForm "Sales_Invoice"
DoCmd.SetWarnings True
Exit Sub


Submit_Click_Err:
MsgBox Error$
Resume Submit_Click_Exit

End Sub
 
use DCount() on your sales table if same customer (on specific date?) has an invoice.
 
use DCount() on your sales table if same customer (on specific date?) has an invoice.
I have the DCount()
The code still runs
The Save file to still appears
You then need to cancel
Then a box appears "The output action was canceled"

I just want to stop the code if the user does not make a selection before running the code

Before the code runs, I need to check for data

I have no data cancel on the report though
 
Define that! Do you have empty tables?
There is no empty tables but you need to make a selection first before running the code
If no record is selected and the user runs the code I get the no data error

I need to stop the code if no selection has been made
 
what is [CustomerName]? combobox? if so:
Code:
Private Sub Submit_Click()

Dim StrFileName As String
Dim StrSaveFile As String
If Forms![Sales_Invoice]![CustomerName].ListIndex < 0 Then
    Exit Sub
End If
StrFileName = strGetFileFolderName("Sales Invoice" & " - Customer -" & " " & Forms![Sales_Invoice]![CustomerName], 2, "Excel")

If Len(StrFileName & "") < 1 Then
StrFileName = Application.CurrentProject.Path & "\Sales Invoice" & Format(Date, "yyyymmdd") & ".xls"
End If

DoCmd.SetWarnings False
DoCmd.OpenQuery "Update Document Sequence - SI"
DoCmd.OpenQuery "Update Transactions- Commercial SI"
DoCmd.OpenQuery "Update Transactions - Commercial - Invoice Number"
DoCmd.OpenQuery "Update Payments from Invoice"

DoCmd.OutputTo acReport, ("Sales Invoice"), acFormatPDF, ("Sales Invoice" & " - Customer Name -" & " " & Forms![Sales_Invoice]![CustomerName] & ".PDF")

Submit_Click_Exit:


DoCmd.Close , ""
DoCmd.OpenForm "Sales_Invoice"
DoCmd.SetWarnings True
Exit Sub


Submit_Click_Err:
MsgBox Error$
Resume Submit_Click_Exit

End Sub
 
General tip: Enable submit button only when all required content is present.
 
if no selection has been made
In what (in which object) exactly?

You should try to formulate a complete and coherent logic. A programmatic solution is nothing more than an implementation of this logic in the code of the language used. If your logic falls out of the lottery wheel in fragments, your code will look exactly like this.
 
what is [CustomerName]? combobox? if so:
Code:
Private Sub Submit_Click()

Dim StrFileName As String
Dim StrSaveFile As String
If Forms![Sales_Invoice]![CustomerName].ListIndex < 0 Then
    Exit Sub
End If
StrFileName = strGetFileFolderName("Sales Invoice" & " - Customer -" & " " & Forms![Sales_Invoice]![CustomerName], 2, "Excel")

If Len(StrFileName & "") < 1 Then
StrFileName = Application.CurrentProject.Path & "\Sales Invoice" & Format(Date, "yyyymmdd") & ".xls"
End If

DoCmd.SetWarnings False
DoCmd.OpenQuery "Update Document Sequence - SI"
DoCmd.OpenQuery "Update Transactions- Commercial SI"
DoCmd.OpenQuery "Update Transactions - Commercial - Invoice Number"
DoCmd.OpenQuery "Update Payments from Invoice"

DoCmd.OutputTo acReport, ("Sales Invoice"), acFormatPDF, ("Sales Invoice" & " - Customer Name -" & " " & Forms![Sales_Invoice]![CustomerName] & ".PDF")

Submit_Click_Exit:


DoCmd.Close , ""
DoCmd.OpenForm "Sales_Invoice"
DoCmd.SetWarnings True
Exit Sub


Submit_Click_Err:
MsgBox Error$
Resume Submit_Click_Exit

End Sub
I get n Object does not support the property or method
run time error 438

If Forms![Sales_Invoice]![CustomerName].ListIndex < 0 Then
 
Last edited:
I thought of something like this:

Code:
private sub SetSubmitEnableMode()

    dim EnableSubmit as boolean

    EnableSubmit= Condition1
  
    if EnableSummit then
        EnableSubmit = Condition2
        ...
    end if

    me.Submit.Enabled = EnableSubmit

end Sub

private sub CustomerName_AfterUpdate()
      SetSubmitEnableMode
end sub

private sub Anything_AfterUpdate()
      SetSubmitEnableMode
end sub

private sub Form_Current()
     SetSubmitEnableMode
end Sub
 

Users who are viewing this thread

Back
Top Bottom