I have a form. The form fields are validated in the querysave event. The validation goes like this. I have some fields to be validated for its presence during save. i.e., when i click a check box and dont enter the details in its field, it should show a error message box while saving. The validation works fine for a new document. My question is
how to make it work for both new document and edit mode?
The error message is not getting displayed the second time when i click on save.i.e., when i click ok in the messagebox, dont enter data and click save, its getting saved. how to make it check for validation everytime when clicked on save.
Kindly help me. Please dont mind if my questions are obvious and simple because i m a fresher. Thanks in advance.
The script goes below,
The first part calculating ref num for new docs and secong part validating fields,
Sub Querysave(Source As Notesuidocument, Continue As Variant)
Dim w As New notesuiworkspace
Dim uidoc As notesuidocument
Set uidoc = w.CurrentDocument
Dim SESS As New NotesSession
Dim Doc As NotesDocument
Dim RefView As NotesView
Dim DB As NotesDatabase
Dim RefDoc As NotesDocument
Set DB = SESS.CurrentDatabase
Set Doc = uidoc.Document
Set RefView = DB.GetView("System\AutoNo")
If uidoc.IsNewDoc = True Then
Financial_year = Clng(Right$(Cstr(Year(Now)),3)) + 104
If Month(Now) >= 4 Then Financial_year = Financial_year + 1
Application = "ST"
DefKey$ = Cstr(Financial_year)
DefNo& = 0
Set RefDoc = RefView.GetDocumentByKey(DefKey$ , True)
If Not(RefDoc Is Nothing) Then DefNo& = Clng(Right$(RefDoc.SETTLEMENT_NO(0),5))
DefNo& = DefNo& + 1
RefNo$ = (Application + DefKey$) & "-" & Right$("00000" & Cstr(DefNo&) ,5)
Doc.SETTLEMENT_NO= RefNo$
Doc.FinFlag="Finish"
Call SESS.SetEnvironmentVar("ENV_SETT",Right$("00000" & Cstr(DefNo&) ,5))
Call uidoc.Refresh
Else
Exit Sub
End If
get_ex_rate
get_cv_local
set_flag
Dim answer2 As Integer
answer2% = Msgbox("Do you want to save this document?", 1, "Save")
If answer2 = 1 Then
Petro$= uidoc.FieldGetText("Park_Petro_Car")
Vehicle$= uidoc.FieldGetText("Vehicle_No")
Gifts$ = uidoc.FieldGetText("Gifts")
Gifts_Ent$ = uidoc.FieldGetText("Gifts_Ent")
Medical$ = uidoc.FieldGetText("Medical")
Medical_Fee$ = uidoc.FieldGetText("Medical_Fee")
Others$= uidoc.FieldGetText("Others")
OS$= uidoc.FieldGetText("Others_Specify")
Taxi$ = uidoc.FieldGetText("Taxi")
Taxi_Fee$ = uidoc.FieldGetText("Taxi_Fee")
If Petro$ <> "" And Vehicle$ = "" Then
Msgbox "Please enter Vehicle No" , 16, "Vehicle No"
Else
If Gifts$ <> "" And Gifts_Ent$ = "" Then
Msgbox "Please enter Guest/Co.Name" , 16, "Guest/Co.Name"
Else
If Medical$ <> "" And Medical_Fee$ = "" Then
Msgbox "Please enter Medical_Fee" , 16, "Medical_Fee"
Else
If Taxi$ <> "" And Taxi_Fee$ = "" Then
Msgbox "Please enter Taxi Fee" , 16, "Taxi Fee"
Else
If Others$ <> "" And OS$ = "" Then
Msgbox "Please enter Others(Specify)" , 16, "Others (Specify)"
End If
End If
End If
End If
End If
End If
If answer2 = 2 Then
continue=False
Exit Sub
End If
uidoc.Refresh
'uidoc.close
End Sub
Remove the Else from your first IF Statement, otherwise the validation only runs once, when IsNewDoc returns True, once the doc has been saved once it will return False and your QuerySave Subroutine exits.
ELSE
Exit Sub <-- remove this, your validation code only runs once per document.
End IF
Thanks for adding the code.
With the "If uidoc.IsNewDoc = True Then" you explicitly tell the code to only run when the document is new.
So either add an appropriate elseif branch or get rid of the if itself and modify the validation accordingly so it applies to new and modified documents.
Related
I am having trouble handling the error associated with a input box "Cancel" click. Or in otherwords, it returns an error within the sub if the value of the input is null. I have tried looking around and still can't seem to get it quite right. Here is my attempt:
Private Sub bttnSavingsExpected_Click()
Dim expected() As String
Dim nPeriods As Integer
Dim counter As Integer
Dim savings As Single
With ActiveSheet.Range("A13")
nPeriods = Range(.Offset(1, 0), .End(xlDown)).Rows.Count
End With
ReDim expected(1 To nPeriods)
counter = 1
For counter = 1 To nPeriods
expected(counter) = Range("A13").Offset(counter, 0).Value
Next
TryAgain:
On Error GoTo ErrH
counter = 1
For counter = 1 To nPeriods
savings = InputBox("How much savings do you expect from " & expected(counter) & "?", "Savings?", Range("A13").Offset(counter, 1).Value)
If savings = "" Then
Exit Sub
Else
Range("A13").Offset(counter, 1).Value = savings
End If
Next
Exit Sub
ErrH:
MsgBox "Please enter value. If the default value is desired then please click 'OK'.", vbOKOnly, "Do Not Click Cancel"
GoTo TryAgain
End Sub
With this attempt, the MsgBox is displayed the first click whether there is a input or not and even if I click "Ok". The second try of clicking "OK" or "Cancel" leads to being kicked back to the editor.
You've got Dim savings As Single and If savings = "" Then. Thats always going to error
Try using Dim savings As Variant
Make sure the variable for the Inbox is set at "", then test the value for False. Much easier than anything else I have seen:
Sub WolfPackURL_input()
Dim TheURL As String
Dim SaveURL As Hyperlink
Set savedURL = Sheets("Data").Range("I1")
TheURL = ""
TheURL = Application.InputBox("Input the Sign-Up URL", "Wolfpack Weekly Players URL", "http://something", 1)
If TheURL = "False" Then
Exit Sub
End If
ThisWorkbook.Worksheets("Data").Activate
Sheets("Data").Range("I1").Hyperlinks.Delete
Sheets("Data").Range("I1").ClearContents
Sheets("Data").Range("I1").Clear
ActiveSheet.Hyperlinks.Add anchor:=Sheets("Data").Range("I1"), Address:=TheURL, ScreenTip:="Open file", TextToDisplay:=TheURL
End Sub
Can anyone tell me how to improve this macro?
All the macro does is it just reads an Excel file for a list a accounts to update in an application (SmarTerm Beta). It technically already accomplishes the goal, but is there a way to code it so that while it’s reading the Excel file, the coordinates of the cells from which to read the account numbers and also the coordinates of the cells in which to write an output don’t depend on a "pre-selected" a cell? The risk with selecting a cell is that if someone were to accidentally select a different cell while the macro is running, everything will get screwed up.
Here's my current code:
Public oExcelObj As Object
Function WaitSystem(Optional NoDialog as Variant) As Boolean
Dim nContinue as Integer
Dim nTimeOut as Integer 'In seconds.
'The default timeout for each command is 3 minutes.
'Increase this value if your host requires more time
'for each command.
nTimeOut = 10
If IsMissing(NoDialog) then NoDialog = False
'Wait for response from host.
Session.EventWait.Timeout = nTimeOut
Session.EventWait.EventType = smlPAGERECEIVED
Session.EventWait.MaxEventCount = 1
WaitSystem = True
If Session.EventWait.Start = smlWAITTIMEOUT Then
If NoDialog Then
WaitSystem = False
Else
nContinue = QuerySyncError()
If nContinue <> ebYes then WaitSystem = False
End If
End If
Set LockStep = Nothing
End Function
'Establish link. Search for Excel.
Function OleLinkConnection
Const XlMaximized = &HFFFFEFD7
Titlebar$ = AppFind$("Microsoft Excel")
If Titlebar$ <> "" Then
bIsExcelActive = True
If AppGetState(Titlebar$) = ebMinimized Then
AppSetState 2, Titlebar$
End If
Else
bIsExcelActive = False
End If
If bIsExcelActive Then
'Create Excel Object using current instance of Excel.
Set oExcelObj = GetObject(, "Excel.Application")
Else
'Create Excel Object using a new instance of Excel.
Set oExcelObj = CreateObject("Excel.Application")
End If
Version = oExcelObj.Application.Version
oExcelObj.ScreenUpdating = True
oExcelObj.Displayalerts = True
oExcelObj.Visible = true
End Function
Sub JPBmacro
Dim AccountNumber As String
Dim Temp As Integer
Begin Dialog StartDialogTemplate ,,211,74,"Run JPBmacro?"
OKButton 60,12,92,20,.Proceed
CancelButton 60,40,92,20,.Exit
End Dialog
Dim StartDialog As StartDialogTemplate
r% = Dialog(StartDialog)
If r% = 0 Then End
g$ = "G:\DATA\outputfile.xlsx"
oleCode = OleLinkConnection
oExcelObj.Workbooks.Open g$
oExcelObj.Range("A1").Select ‘<----This selects the cell from which all coordinates are based off of. The coordinates of oExcelObj.ActiveCell.Offset(Y,X).Value VBA depend on selecting a cell.
NEXTACCOUNT:
Temp = 0
AccountNumber = oExcelObj.ActiveCell.Offset(Temp,0).Value
While AccountNumber <> ""
Session.SendKey "CLEAR"
If WaitSystem = False Then End
Session.Send "ACTU " & AccountNumber
Session.SendKey "ENTER"
If WaitSystem = False Then End
If Trim(Session.ScreenText(4,6,1,22)) = "INVALID ACCOUNT NUMBER" Or Trim(Session.ScreenText(4,6,1,19)) = "ACCOUNT NOT ON FILE" Then
oExcelObj.ActiveCell.Offset(Temp,1).Value = Trim(Session.ScreenText(4,6,1,22))
GoTo RESTARTLOOP
End If
UPDATEIOV:
If Trim(Session.ScreenText(13,76,1,1)) = "Y" Then
oExcelObj.ActiveCell.Offset(Temp,1).Value = "Account already flagged as institutional."
Else
Session.Row = 13
Session.Column = 76
Session.send "Y"
Session.SendKey "ENTER"
If WaitSystem = False Then End
oExcelObj.ActiveCell.Offset(Temp,1).Value = Trim(Session.ScreenText(24,2,1,50))
End If
RESTARTLOOP:
Temp = Temp + 1
AccountNumber = oExcelObj.ActiveCell.Offset(Temp,0).Value
Wend
ENDNOW:
oExcelObj.Workbooks.Close
MsgBox "All Done!"
End Sub
Why not keep the reference to the first cell?
Dim rng as Range
Set rng = oExcelObj.Range("A1")
i=1
...
x = rng.Cell(i,1).Value
'Or faster yet is reading all the values into an variant array.
Dim array() as Variant
array = rng.Resize(N,M).Value
' Work with array as
x = array(i,1)
Given the comment from assylias and that another poster has since "answered" with this approach:
I can't see where oExcelObj is instantiated? Or how you are referring to a specific sheet.
Regardless of which,
you can avoid select by setting a range, ie Set rng1 = oExcelObj.Sheets(1).Range("A1")
and then use offsets from rng1.
The user won't be able to interfere while the code is running
I have an Add button in the dialog form to add items, its quantity, price , currency and list in the field below. There is a currency field in the form. it is a drop down list with many currencies. The currency should be same on adding the items. if there is currency change, message box should appear. below is the part of the code for add button event. "cur" is the currency field.
Sub Click(Source As Button)
'On Error Goto errhandle
Dim work As New notesuiworkspace
Dim uidoc As notesuidocument
Dim doc As notesdocument
Dim item As String, weight As String
Dim qty As String, price As String
Dim sbtotal As String
Dim gtotal As String
Set uidoc = work.currentdocument
Set doc =uidoc.Document
item = uidoc.FieldGetText("Item")
qty = uidoc.FieldGetText("Qty")
price = uidoc.FieldGetText("Price")
cur = uidoc.FieldGetText("cur")
sbtotal= uidoc.FieldGetText("SubTotal")
Call uidoc.Refresh
'weight = uidoc.FieldGetText("W_Qty")
'adj = uidoc.fieldGetText("Adj")
remark = uidoc.FieldGetText("Remarks")
If item = "" Or qty = "" Or price = "" Then
Msgbox "Please complete the data entry ", 16, "Error - Incomplete Data Entry"
Exit Sub
End If
recordNo = uidoc.fieldgettext("ww")
If recordNo = "" Then
recordNumber = 0
Else
pos = Instr(recordNo,";")
If pos > 0 Then
number = Right(recordNo , pos -1)
Else
number = Left(recordNo , pos +1)
End If
recordNumber = Cint(number)
End If
recordNumber = recordNumber + 1
'to append text
Call uidoc.FieldAppendText("no" ,";" & Cstr(recordNumber))
Call uidoc.FieldAppendText("Item1" ,";" & item)
Call uidoc.FieldAppendText("Q1" , ";" & Cstr(qty))
Call uidoc.FieldAppendText("amt" , ";" & Cdbl(price))
Call uidoc.FieldAppendText("C1" , ";" & Cstr(cur))
Call uidoc.FieldAppendText("TSubTotal" , ";" & Cdbl(sbtotal))
'clear entering data
uidoc.FieldClear("Remarks")
uidoc.FieldClear("Item")
uidoc.FieldClear("Qty")
uidoc.FieldClear("Price")
'uidoc.FieldClear("W_Qty")
Call uidoc.FieldSetText("SubTotal","0.00")
uidoc.refresh
Dim subtotal As Double
subtotal = 0
Forall stotal In doc.TSubTotal
If stotal <> "" Then
subtotal = subtotal + Cdbl(stotal)
End If
End Forall
total = subtotal '+ Cdbl(curdoc.SubTotal(0))
Call uidoc.FieldSetText("GrandTotal",Format(total,"#,##0.00"))
uidoc.refresh
uidoc.gotofield"Item"
End Sub
Please help me. Thanks.
Create a new hidden field called selectedCurrency. The initial value of this field should be empty.
In your Add button code, you need to first check selectedCurrency, and if it is blank you should set it equal to cur.
Then, also in the code for the Add button, you need to compare selectedCurrency and cur, and if they are not equal you should display your message box.
I'd fix the currency outside the code for the Add button, and also make it required before Add can be started.
I use the below script in querysave event of a form. The logic is when I save the form the sequence should get displayed in the view in two columns. like "115-" in one column and the sequence "00001", "00002", ... in the second column. The first two documents gets saved without any issue. When I save try to save 3rd and more documents, its displaying "00002" only every time after that. I am not able to identify what is the mistake. Can somebody help please.
Sub Querysave(Source As Notesuidocument, Continue As Variant)
Dim SESS As New NotesSession
Dim w As New NotesUIWorkspace
Dim uidoc As NotesUIdocument
Dim Doc As NotesDocument
Dim RefView As NotesView
Dim DB As NotesDatabase
Dim RefDoc As NotesDocument
Set DB = SESS.CurrentDatabase
Set uidoc = w.CurrentDocument
Set Doc = uidoc.Document
Set RefView = DB.GetView("System\AutoNo")
Dim approvedcnt As Integer
approvedcnt = Cint(source.fieldgettext("appcnt"))
If uidoc.EditMode = True Then
Financial_Year = Clng(Right$(Cstr(Year(Now)),3)) + 104
If Month(Now) >= 4 Then Financial_Year = Financial_Year + 1
DocKey = Cstr(Financial_Year)& "-"
New_No = 0
Set RefDoc = RefView.GetDocumentByKey(DocKey , True)
If Not(RefDoc Is Nothing) Then New_No = Clng(Right$(RefDoc.SETTLEMENT_NO(0),5))
New_No = New_No + 1
autono = DocKey & "-" & Right$("00000" & Cstr(New_No) ,5)
Application ="ST"
Latest_No = Application + autono
Doc.SETTLEMENT_NO = Latest_No
Doc.FinFlag="Finish"
Call SESS.SetEnvironmentVar("ENV_ST_NO",Right$("00000" & Cstr(DefNo&) ,5))
'Call uidoc.FieldSetText("SETTLEMENT_NO",Latest_No)
Call uidoc.Refresh
Else
Exit Sub
End If
get_ex_rate
get_cv_local
Call uidoc.FieldSetText("Flag1", "A")
If approvedcnt = 12 And uidoc.FieldGetText("STATUS") = "APPROVE" Then
Call uidoc.fieldsettext("Flag2", "B")
End If
Dim answer2 As Integer
answer2% = Msgbox("Do you want to save this document?", 1, "Save")
If answer2 = 1 Then
Print "Saving"
End If
If answer2 = 2 Then
continue=False
Exit Sub
End If
uidoc.Refresh
uidoc.close
End Sub
I imagine your call to GetDocumentByKey is getting the wrong document or not the next one in sequence. Make sure the view is sorted properly and perhaps call the refresh method on the view before calling GetDocumentByKey.
Good day,
I'll start by saying I work for a small company and have no official training in
Notes everything I know I've learned buy trial and error & using other peoples codes.
Application: We have a purchase order database that has been running for a very long time and threw the ages people put in supplier names diffirently. Now I found a code that goes into the selected forms and changes the field values which is exactly what I need the only problem
is it's single line. The field I want to update have about 5 text lines (Company name, Tel No etc..) and the original programer put all off the info in one field.
Question: Is there a way in the script linked below how I can make each prompt input go into a diffirent line.I tried a few thing and I think I may be missing something obvious.(If I try chr(10);chLv all I get is either the 2 values next to each other or get them seperated by a comma)
`
Sub Initialize
Dim ws As New NotesUIWorkspace
Dim session As New NotesSession
Dim prompt As String
Dim fieldName As String
Dim fieldValue As String
Dim dataTypes As Variant
Dim thisDataType As String
Dim fieldValues As Variant
Dim newFieldValues As Variant
Dim db As NotesDatabase
Dim coll As NotesDocumentCollection
Dim i As Integer
Dim doc As NotesDocument
Dim item As NotesItem
prompt = "Please enter the name of the field to be updated"
fieldName = ws.Prompt(3, "Enter Field Name", prompt, "")
If fieldName = "" Then Exit Sub
If Instr(fieldName, " ") <> 0 Then
prompt = "Error! Field Names can't have spaces!"
Msgbox prompt, 16, "Error"
Exit Sub
End If
prompt = "Please enter the new value. For multiple values, separate with a colon."
Value1 =ws.Prompt(3, "Enter Field Value", prompt, "")
Value2= ws.Prompt(3, "Enter Field Value", prompt, "")
Fieldvalue=value1 + Chr(10) +value2
Redim dataTypes(5) As String
dataTypes(0) = "Text"
dataTypes(1) = "Number"
dataTypes(2) = "Date"
dataTypes(3) = "Readers"
dataTypes(4) = "Authors"
dataTypes(5) = "DELETE THIS FIELD"
prompt = "Choose the data type of the value(s)"
thisDataType = ws.Prompt(4, "Choose Data Type", prompt, dataTypes(0), dataTypes)
If thisDataType = "" Then Exit Sub
Set db = session.CurrentDatabase
Set coll = db.UnprocessedDocuments
fieldValues = Evaluate({#Explode("} & fieldValue & {"; ":")})
Select Case thisDataType
Case dataTypes(0) : Redim newFieldValues(Ubound(fieldValues)) As String
Case dataTypes(1) : Redim newFieldValues(Ubound(fieldValues)) As Double
Case dataTypes(2) : Redim newFieldValues(Ubound(fieldValues)) As Variant
Case dataTypes(3) : Redim newFieldValues(Ubound(fieldValues)) As String
Case dataTypes(4) : Redim newFieldValues(Ubound(fieldValues)) As String
End Select
For i = Lbound(fieldValues) To Ubound(fieldValues)
Select Case thisDataType
Case dataTypes(0) : newFieldValues(i) = Trim(fieldValues(i))
Case dataTypes(1) : newFieldValues(i) = Val(fieldValues(i))
Case dataTypes(2) : newFieldValues(i) = Cdat(fieldValues(i))
Case dataTypes(3) : newFieldValues(i) = Trim(fieldValues(i))
Case dataTypes(4) : newFieldValues(i) = Trim(fieldValues(i))
End Select
Next
Set doc = coll.GetFirstDocument
While Not doc Is Nothing
If thisDataType = "DELETE THIS FIELD" Then
If doc.HasItem(fieldName) Then Call doc.RemoveItem(fieldName)
Else
Call doc.ReplaceItemValue(fieldName, newFieldValues)
If thisDataType = dataTypes(3) Or thisDataType = dataTypes(4) Then
Set item = doc.GetFirstItem(fieldName)
If thisDataType = dataTypes(3) Then item.IsReaders = True
If thisDataType = dataTypes(4) Then item.IsAuthors = True
End If
End If
Call doc.Save(True, False)
Set doc = coll.GetNextDocument(doc)
Wend
End Sub
'
Sorry for the long post but wasn't sure what is needed. First time posting for help but I'm scared I missed something opposite.
Francois
If you don't care that the values are not actually seperate, but are in fact a single string seperated by newlines, you could try evaluating Field fieldname:=#implode(fieldname, #newline) There was a bug (now fixed) in the java api where using java newline chars \n did not translate through to the stored value. Having the field set via evaluating an #formula was a workaround.
It's possible (?) that there is a platform specific issue to using Chr(10). Have you tried using Chr(13) & Chr(10)? You coudl also try evaluating #newline and using what that gives you.
To display the values in separate lines within a field you have to open the field properties and on the 3rd Tab make sure the option "Display separate values with" is set to "New Line".
On another note the split function in lotusscript is the equivalent to the #explode, so this line:
fieldValues = Evaluate({#Explode("} & fieldValue & {"; ":")})
can be modified to the following:
fieldValues = split(fieldValue, ":")
Hope that helps.