SAP GUI script handling two possible popups when saving - vba

Created a script for price update on Purchasing Orders (PO's).
A problem occurs if price I'm trying to update is already same on the PO. Then there is nothing to save and Information popup windows appears saying "No data change". Here is where the script stops working as recording of the script is based on that new price is different and it can be saved.
I tried to add if statement to the code but it's not working. It doesn't save when the new price is different and it needs to be saved.
It always chooses Enter and Else statement is not utilized.
session.findById("wnd[0]/tbar[0]/btn[11]").press 'Save on Menu bar
If session.ActiveWindow.Name = "wnd[1]" Then
If session.findById("wnd[1]").text = "Information" then
session.findById("wnd[1]/tbar[0]/btn[0]").press 'If popup and it's only Information type press Enter
objSheet.Cells(i, 3).Value = "No data changed" 'Information that no change has been done
Else
session.findById("wnd[1]/usr/btnSPOP-VAROPTION1").press 'Save in the popup window
objSheet.Cells(i, 3) = "Updated" 'Information that the new price has been updated
End if

By removing If session.ActiveWindow.Name = "wnd[1]" Then the script works as it should
If session.findById("wnd[1]").text = "Information" then
session.findById("wnd[1]/tbar[0]/btn[0]").press 'If popup and it's only Information type then press Enter
objSheet.Cells(i, 3).Value = "No data changed" 'Information that no change has been done
Else
session.findById("wnd[1]/usr/btnSPOP-VAROPTION1").press 'Save in the popup window
objSheet.Cells(i, 3) = "Updated" 'Information that the new price has been updated
End if

Related

Form closes but the written conditions therein not implemented

I have the following two codes on a button: The (first code) aims to submit value of option button into worksheet cell:
For Each FormControl In Me.Controls
'Check only OptionButtons
If TypeName(FormControl) = "OptionButton" Then
'Check the status of the OptionButton
If FormControl.Value = True Then
'Set a variable equal to the Caption of the selected OptionButton
OptionButtonValue = FormControl.Caption
'We found the selected OptionButton so exit the loop.
Exit For
End If
End If
Next
'Store input in the worksheet
Sheets("Answer Sheet").Range("E80").Value = OptionButtonValue
To ensure an option button is selected before proceeding to next form, i have
the 'following code (second code):
Dim cnt As Integer
For Each ctl In Me.Controls
If TypeName(ctl) = "OptionButton" Then
If ctl.Value = True Then cnt = cnt + 1
End If
Next ctl
If cnt = 0 Then MsgBox "Hello " & CStr(ThisWorkbook.Sheets("AccessReg").Range("D630").Value) & ", you
have not selected an answer! Please select an answer to proceed to next question. Thank you.",
vbInformation, "Please select an answer!" Else ScoreBoards.Show
Unload Me
MY CHALLENGES
Both codes above exists in my forms i.e. questions 1, 2, 3,...respectively, but can't seem to get the second code (that, which ensures an option button is selected before next form can be opened) to work by adding 'unload me' to the end of it, yet i want the form closed before proceeding to next. Adding 'unload me', pop-up the msgbox (which tells me to select an answer) but when i clicked okay on the msgbox, it closes the form (Question1) instead of returning me to same form to ensure an answer is clicked, then proceed to next form (Question2). However, when i remove the 'unload me', things work fine i.e. the msgbox popup when selection not made, returns to same form when okay on msgbox is clicked, and opens next form when selection made.
What i really want is: i want the second code above (which ensures an option button is selected before next form can be opened) to work as programmed and each form closed before proceeding to the next form.
Thank you in advance
PS:
The concept summary is:
On a userform (Question1), select an option button and submit the value to worksheet
Ensure an option button is selected:
if selected and button clicked, the next form(being Question2) should open
if not selected and button clicked, the msgbox (which tells me to select an answer), should popup
clicking okay on the msgbox, should return me to same form (Question1) so that i can select an option and proceed.
Try adapting your second code in the next way, please:
If cnt = 0 Then MsgBox "Hello " & _
CStr(ThisWorkbook.Sheets("AccessReg").Range("D630").Value) & ", you have not selected an answer! Please select an answer to proceed to next question. Thank you.", vbInformation, "Please select an answer!"
Exit For
Else
ScoreBoards.Show
Unload Me
End if

MS-ACCESS Write Conflict Issue

I am having a write conflict issue. Not sure what the cause could be. The issue occurs when multiple users are using the Access database.
Below is a schematic of whats happening. The (Main) Form displays a Subform (with some data displayed from the Table). The user can double click a name to pull up that individuals details, via the (Details) Form that pops up. If changes are made, the user can save the details (actually, a prompt pops up asking if you would like to save when closing the window).
When saving, then going to open up another individuals details, the write conflict pops up.
The code when closing the (Details) Form:
Private Sub Form_BeforeUpdate(Cancel As Integer)
' This procedure checks to see if the data on the form has
' changed. If the data has changed, the procedure prompts the
' user to continue with the save operation or to cancel it. Then
' the action that triggered the BeforeUpdate event is completed.
Dim ctl As Control
On Error GoTo Err_BeforeUpdate
' The Dirty property is True if the record has been changed.
If Me.Dirty Then
' Prompt to confirm the save operation.
If MsgBox("Do you want to save?", vbYesNo + vbQuestion, _
"Save Record") = vbNo Then
Me.Undo
End If
End If
Exit_BeforeUpdate:
Exit Sub
Err_BeforeUpdate:
MsgBox Err.Number & " " & Err.Description
Resume Exit_BeforeUpdate
End Sub
Which was taken from the Microsoft website (I think).
Is there something wrong with my data structure that is causing this?
If more information is needed, I can edit the post to include more code etc.
EDIT 1: Reversed Subform arrow. The Subform simply displays data from the Table.
EDIT 2: Backend is Access. To open the (Details) Form from the Subform:
Private Sub First_Name_DblClick(Cancel As Integer)
DoCmd.OpenForm "Details", , , "[First Name] = '" & Me![First Name] & "' and [Last Name] = '" & Me![Last Name] & "'"
End Sub
EDIT 3:
While looking at the (Main) Form, the Subform has a Source Object set to the name of the Subform, which I call QuerySF. The QuerySF Form has a Record Source set to the Table. The pop-up (Details) Form has a Record Source set to the Table. I can instead set the Record Source for the (Details) Form to my Query, but I tried this and it did not help.
Would inserting a Requery somewhere help?
I have noticed the pop-up occurs every third time the (Details) Form is opened via the Subform.
If you don't want to save, use Cancel = True. BeforeUpdate would not trigger if form is not dirty so don't bother with If Me.Dirty.
' Prompt to confirm the save operation.
If MsgBox("Do you want to save?", vbYesNo + vbQuestion, "Save Record") = vbNo Then
Cancel = True
Me.Undo
End If

"Undo" ComboBox DropButtonClick

I have a series of ComboBoxes that are populated based on the previous ComboBox selection. So for example, ComboBox3 is populated based on the value selected in ComboBox2. The "trigger" for populating ComboBox3 is the DropButtonClick action. I have a message box pop up when there is no value in ComboBox2. This is working successfully - code below.
If Me.ComboBox2.ListIndex = -1 Then
MsgBox "Please select all preceding comboboxes"
ComboBox3.Value = ""
Exit Sub
Else
sh.Range("B2") = Me.ComboBox2.Value
End If
My issues is once the Message Box appears (as a result of there being no value in ComboBox2) ComboBox3 still displays drops down values. Is there a way to undo the ComboBox3 DropButtonClick Event when there is no value in ComboBox2, so that ComboBox3 never drops down?
You can immediately close the drop-down window of the combo-box by simulating the "ESC" key:
MsgBox "Please select all preceding comboboxes"
ComboBox3.Value = ""
' close immediately the combo's dropdown window
SendKeys "{ESC}{ESC}"
Is this something for you?
If Me.ComboBox2.Value = "" Then
ComboBox2.SetFocus
Else
sh.Range("B2") = Me.ComboBox2.Value
End If
When you want to click on the dropbutton of combobox3 it automaticly goes back to combobox2.

Excel "Workbook_BeforeClose" event not firing again after canceled

Update: After more research, I found this duplicate question: Excel 2016 Workbook.BeforeClose event firing every other time bug. It seems I was using the wrong keywords, and that this is a bug, not a problem with my code. However, I cannot seem to download the version mentioned in the solution. I am running Windows 7 and using Microsoft Office 365 Pro Plus, and Office is stating that the most up to date version available is 16.0.6965.2105
I am trying to use the Workbook_BeforeClose event to test whether a checkbox is checked or not. If not, the user is prompted on whether they want to continue closing with out checking the box. If they choose "Yes," the sheet is cleared and the workbook is saved. If they choose, "No," the box is checked and, "Cancel" is set to true.
This works fine the first time the Workbook_BeforeClose event runs. However, the second time the sheet is closed, the standard Excel "Want to save your changes to..." dialogue box comes up and the Workbook_BeforeClose event does not fire. If I click cancel on the dialogue box and close the workbook a third time, the event fires. Something is being reset when, "Cancel," is clicked in the dialogue box, but I can't figure out what it is. My code is below:
Public Closing as Boolean
Sub Workbook_BeforeClose(Cancel As Boolean)
Debug.Print "Workbook_BeforeClose"
If Closing = True Then Exit Sub 'Closing is used as a switch to stop the event from looping on Application.ThisWorkbook.Close below
Closing = True
If Sheets(1).DraftCheckBox = False Then
If MsgBox("This file is not being saved as a draft. This workbook will be cleared if the draft box is not checked." & vbCr & vbCr & "Would you like to continue?", vbYesNo, "Warning") = vbYes Then
'If "Yes" selected
'Stuff happens here
Application.ThisWorkbook.Close savechanges:=True
Else
'If "No" selected
Sheets(1).DraftCheckBox = True
Cancel = True
End If
End If
Closing = False
End Sub
I know for a fact that Application.EnableEvents is set to True. I also know that the event itself is not firing because there is a stop on the very first line at "Debug.Print" This stop is activated after the first close and I can step through the code. It does not activate at all after the second close, either before or after the dialogue box. Is there anything that I should be doing to prevent the dialogue box from coming up and to force the Workbook_BeforeClose event every time the workbook is closed?
You are already closing the workbook you don't want to call close again, use Save instead.
You also don't need the extra variable if you use the code below. One big question remains...how is your user imitating the Close event? If by the red X then the code below should work. If by a button on your form what does that button code look like?
Note: This is untested code as I don't have your workbook.
Sub Workbook_BeforeClose(Cancel As Boolean)
If Sheets(1).DraftCheckBox = False Then
If MsgBox("This file is not being saved as a draft. " + _
"This workbook will be cleared if the draft box is not checked." _
& vbCr & vbCr & "Would you like to continue?", _
vbYesNo, "Warning") = vbYes Then
'If "Yes" selected
'Stuff happens here
Application.ThisWorkbook.Save
Cancel = False 'Just to be sure.
Else
'If "No" selected
Sheets(1).DraftCheckBox = True
Cancel = True
End If
End If
End Sub
HTH

TabIndex is working incorrectly

The Goal:
When I switch pages in my VBA userform, I would like a certain field to be the first input value. And then I want them to switch sequentially when I tab through them.
What I have done so far:
The pages are created (for the most part, they still need an artistic mind to make it look like an engineer didn't do it), the logic is made, basically everything is done. The input fields tabIndex propertys are set starting at 20 and going up to 27 in the order I want them in. Pictures will be attached below.
The Problem:
When I push the next button, the userform automatically goes to the "Cooling system" drop down. If I push tab, it goes to the next dropdown. Then through the other options in a strange but constant order. And the weird thing is, I can type but no text appears. The cursor moves, but nothing comes behind it. After I have tabbed through everything, if I push tab again only THEN does it do what I want it to do. I have attached pictures and the code for the next button. Thanks for your help in advanced. Let me know if I can clarify anything!
The Page that works fine:
The problem page (I tried to show how nothing shows when you type and it goes to that drop down automatically):
Code for "Next" button:
Private Sub bNextSystem1_Click() 'checks to see if all parameters are entered
If IsNumeric(Me.txtS1elec.value) = True And IsNumeric(Me.txtS1NG.value) = True And IsNumeric(Me.txtS1sqft.value) = True Then
If Me.txtS1elec.value <> "" And Me.txtS1NG.value <> "" And Me.txtS1sqft.value <> "" And Me.ddS1Cooling.ListIndex > -1 And Me.ddS1Heating.ListIndex > -1 Then
NextPage
Me.txtS1elec.BackColor = vbWhite
Me.txtS1NG.BackColor = vbWhite
Me.txtS1sqft.BackColor = vbWhite
Else
MsgBox "Please check to see if all options are selected or entered."
GoTo CleanFail
End If
Else
HighlightBadCells1 'checks for incorrect cell input values
MsgBox "Please check the highlighted cells"
GoTo CleanFail
End If
Me.txtS2elec.value = Me.txtS1elec.value
Me.txtS2NG.value = Me.txtS1NG.value
CleanFail:
End Sub
Code for "NextPage" Sub routine:
Private Sub NextPage()
MultiPage1.Pages(MultiPage1.value + 1).Visible = True 'hides current page
MultiPage1.Pages(MultiPage1.value).Visible = False 'reveals next page
End Sub
Select the Multipage Page
CLick View -> Tab Oder
This will show the Tab Order Dialog.