Dont run if no Data (1 Viewer)

Gismo

Registered User.
Local time
Tomorrow, 00:43
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
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:43
Joined
May 7, 2009
Messages
19,245
use DCount() on your sales table if same customer (on specific date?) has an invoice.
 

Gismo

Registered User.
Local time
Tomorrow, 00:43
Joined
Jun 12, 2017
Messages
1,298
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
 

Gismo

Registered User.
Local time
Tomorrow, 00:43
Joined
Jun 12, 2017
Messages
1,298
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
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:43
Joined
May 7, 2009
Messages
19,245
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
 

Josef P.

Well-known member
Local time
Today, 23:43
Joined
Feb 2, 2023
Messages
827
General tip: Enable submit button only when all required content is present.
 

ebs17

Well-known member
Local time
Today, 23:43
Joined
Feb 7, 2020
Messages
1,946
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.
 

Gismo

Registered User.
Local time
Tomorrow, 00:43
Joined
Jun 12, 2017
Messages
1,298
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:

Josef P.

Well-known member
Local time
Today, 23:43
Joined
Feb 2, 2023
Messages
827
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

Top Bottom