Not that I'm insisting on my way, but I can type above my main sub;But not test the code by
debug.print GoSub xxx
OrderFromFK=123
Gosub GetEmailHeader
Debug.print msg
Not that I'm insisting on my way, but I can type above my main sub;But not test the code by
debug.print GoSub xxx
@sonic8 For sure you're much better than me in this. So instead of answering your points, I prefer to ask you a question.
Can you explain the benefits of using individual functions when you don't plan to reuse them? (instead of my jump labels)
Thanks.
At last someone understands what I'm talking about.Using the GoSub means that all the recordsets are open and no logic is required to determine whether to open or not and no variables have to be passed around. The IDs of parent records are available as they are added so they can be used as FK's in the child records. The array each record gets loaded into is available for each GoSub to use. Using separate subs or functions adds no value and creates complex coupling requirements to pass all that data around. And since the individual record types are irrelevant out of this context, the code has no reuse options.
Post in thread 'Income Tax Visual Basic' https://www.access-programmers.co.uk/forums/threads/income-tax-visual-basic.315921/post-1744691Kind Galina: "many-many years ago ... " - when? - first PC is just from 1983 (if I remember correctly)
You are too young to say so, many happy years to you!
...
Have you ever calculated the shortest distance (more then 300 miles on the surface of the Geoid) on the Great Circle Arc by a logarithmic ruler?![]()
Unlike most of you, I don't care jumping up and down. I like to create an image of what the whole function is supposed to do. So my code mostly seems like a map. If anyone sees the code, he definitely will understand what I'm trying to do and how.
I like to keep my code clean and tidy. So a train of code from top to down doesn't work for me. I'm not afraid of jumping around like a jack-in-a-box because the editor can be split. The top half of the editor is set to show the map, and the bottom half I can move through different sections of the cod.
Every morning I have to send a mail with an embedded table about previous manufacturing result. The following is a part of the code used for this purpose. I know you pros like you don't like this method, but I absolutely like it.
Code:Dim Msg As String Dim Receiver As String Dim Attachment As String Dim Subject As String Dim CC As String Dim AddAttachment As Boolean Dim Atta As String Dim fltr As String Dim rs As DAO.Recordset If TestMode Then Stop Else On Error GoTo ErrorTrap End If Msg = "" GoSub SetFilter GoSub AddHtmlHeader GoSub AddMailHeader GoSub AddTableHeader GoSub AddTableRows GoSub AddTableEnd GoSub GetSignature GoSub AddAttachment SendMail_Attach True, Receiver, Attachment, Subject, Msg, CC, True, False Exit sub Set Filter: ...... ...... ...... Return AddMailHeader: Msg = Msg & "<p>暲栘恀旤 條<br/>" Msg = Msg & "<p>" Msg = Msg & DLookup("CompanyName", "tblOrderedFrom", "OrderedFrom_PK=" & OrderedFrom_FK) Msg = Msg & " 偐傜怴偟偄拲暥傪捀偒傑偟偨偺偱拲暥彂傪揧晅偟偰傑偡丅<br/>" Msg = Msg & "偍朲偟偄強怽偟栿偁傝傑偣傫偑丄彜嵃偺搊榐傪媂偟偔偍婅偄抳偟傑偡丅<br/><br/></p>" Return AddTableHeader: Msg = Msg & "<table>" Msg = Msg & "<tr>" Msg = Msg & "<th bgcolor = #E0F8F1 WIDTH='130'>庴拲斣崋</th>" Msg = Msg & "<th bgcolor = #E0F8F1 WIDTH='150'>拲斣</th>" Msg = Msg & "<th bgcolor = #E0F8F1 WIDTH='150'>惢斣</th>" Msg = Msg & "<th bgcolor = #E0F8F1 WIDTH='150'>恾斣</th>" Msg = Msg & "<th bgcolor = #E0F8F1 WIDTH='60'>悢検</th>" Msg = Msg & "<th bgcolor = #E0F8F1 WIDTH='80' style='text-align:center'>扨壙</th>" Msg = Msg & "<th bgcolor = #E0F8F1 WIDTH='120'>婓朷擺婜</th>" Msg = Msg & "</tr>" Return AddTableRows: ' Create the table rows Set rs = CreateRs_DAO("qryOrders_Products", fltr) With rs If .EOF Then Return Do Msg = Msg & "<tr>" Msg = Msg & "<td bgcolor = #F5F6CE WIDTH='130'>" & !ID & "</td>" Msg = Msg & "<td bgcolor = #F5F6CE WIDTH='150'>" & !OrderNo & "</td>" Msg = Msg & "<td bgcolor = #F5F6CE WIDTH='150'>" & !ManufacturingNumber & "</td>" Msg = Msg & "<td bgcolor = #F5F6CE WIDTH='150'>" & !DrNo & !OrderDrNoChanges & "</td>" Msg = Msg & "<td bgcolor = #F5F6CE WIDTH='60'>" & !Quantity & "屄" & "</td>" Msg = Msg & "<td bgcolor = #F5F6CE WIDTH='80' style='text-align:right;padding-right:15px'>\" & Format(!UnitPrice, "#,###") &"</td>" Msg = Msg & "<td bgcolor = #F5F6CE WIDTH='120'>" & !Delivery & "</td>" Msg = Msg & "</tr>" .MoveNext Loop Until .EOF End With Return
Anyone who sees the code, can imagine how it works. If I need to change some section of it, I can jump right to the appropriate code.
As I said, the first GoSub Section works like a map for the code. It tells me what I'm trying to do in each step. If I need to change the sequence, I simply can drag a GoSub to a higher or lower place.
I wouldn't use goto's to provide a process framework.Honestly (I'm being sincere, not sarcastic) the easiest way to become convinced to agree with avoiding using it is probably to do this:
Begin using it liberally in code that you're still working on developing. Something that won't be super simple but will evolve a bit and become at least moderately complex. After a while you'll become a believer.
It's really not that there is anything "bad" about a single GoTo. The problem is that GoTo's as a logical construct tend to immediately begin spawning. 1 will turn into 3, and all of a sudden you'll realize that the 3 must become 15 in order for everything to work. Trust me, it becomes a nightmare slippery slope.
I use them once in a great while, but normally there are better ways to design your control-of-flow.
Then again, improving the quality of your control-of-flow is an infinite life's work, as we are always getting better and improving.
For example I personally DO use Exit Function pretty liberally in my functions, while that is recommended to avoid (in favor of taking more time to think it through but I get lazy sometimes).
open resources
batch header
     invoice header(s)
         invoice line(s)
     invoice footer
batch footer
close resources
	@gemma-the-huskyOne construct I do find awkward is returning multiple values from a function.
Sub test()
    Dim Param1 As Date
    Dim Param2 As String
    Dim Param3 As Integer
   
    Param1 = Date
    Param1 = MyFunction(Param1, Param2, Param3)
    Debug.Print Param1
    Debug.Print Param2
    Debug.Print Param3
End Sub
Public Function MyFunction(Param1, Optional ByRef Param2, Optional ByRef Param3) As Date  
        Param2 = "Kita Yama"
        Param3 = 100
        MyFunction = DateAdd("d", 1, Param1)       
End Function
	I personally prefer not to work with public variables.Regarding returning multiple values from one function,
see this post/example by MajP
It's not a public variable. It's a user-defined type.I personally prefer not to work with public variables.
@gemma-the-husky
How about this : You have 3 returning values from one function.
Code:Sub test() Dim Param1 As Date Dim Param2 As String Dim Param3 As Integer Param1 = Date Param1 = MyFunction(Param1, Param2, Param3) Debug.Print Param1 Debug.Print Param2 Debug.Print Param3 End Sub Public Function MyFunction(Param1, Optional ByRef Param2, Optional ByRef Param3) As Date Param2 = "Kita Yama" Param3 = 100 MyFunction = DateAdd("d", 1, Param1) End Function