Archives

First learn stand, then learn fly. Nature rule, Daniel-san, not mine. (VBA)

Miyagi

Typically, “Hello World” is the first bit of programming we all do. It is the simplest, and most basic way to interact with the computer, essentially Marco Polo for adults.

Important: All information on this site is provided “as is” for informational purposes only.
In particular, all code samples are provided “as is” without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

For the first VBA snippet I’m going to post, we’ll do something slightly more involved. Let’s suppose that your superior wants you to verify that the “Ref Name” text box on the Sales Order (SO) screen is filled out. They intend to use it to document a project name for each sales order and they don’t want the users to have the option of leaving it blank.

As I mentioned in my last post, I approach this with small steps and achieve success on each step before continuing. So, begin by opening the SO screen, right click on the mnemonic, choose components, and BEFORESAVE to get to the VBA environment.

The first small step is essentially the same as “Hello World.”

Public Function BEFORESAVE() As Boolean
 If Trim(ThisForm.m2mpageframe1.page1.txtfordername.Value) = "" Then
    MsgBox ("Please specify a project name.")
 End If
 BEFORESAVE = True
End Function

You may ask, “How do I know the name of the text box?” The simplest way is to click on it and hit Ctrl-Shift-R. This will copy the “path” of the control to the clipboard. Incidentally, you have to add the Trim function because the field can contain blank spaces and that isn’t the same thing as “” to a computer. This code simply notifies the user that they have made a mistake, but doesn’t prevent them from doing so. To prevent them from continuing we have to stop the BEFORESAVE event. Also, it would be nice that when we did so, the original screen would come back up with the text box focused so the user could immediately fix their mistake.

Public Function BEFORESAVE() As Boolean
 If Trim(ThisForm.m2mpageframe1.page1.txtfordername.Value) = "" Then
    MsgBox ("Please specify a project name.")
    BEFORESAVE = False
    ThisForm.m2mpageframe1.page1.txtfordername.SetFocus
    Exit Function
 End If
 BEFORESAVE = True
End Function

That bit of code will force the user to enter information into the text box or it will not let them save. Yes, I know this code isn’t exactly Halo, but this is our first project after all.

What if management only wants numbers entered into this field? Well, you could use the following.

Public Function BEFORESAVE() As Boolean
 If Not IsNumeric(Trim(ThisForm.m2mpageframe1.page1.txtfordername.Value)) Then
    MsgBox ("The Ref Name field can only contain numbers.")
    BEFORESAVE = False
    ThisForm.m2mpageframe1.page1.txtfordername.SetFocus
    Exit Function
 End If
 BEFORESAVE = True
End Function

As you can see, the code is not difficult to read and understand. In a previous post, I mentioned that one of the negatives of using VBA in Made2Manage is that it doesn’t handle error checking very well. That isn’t entirely accurate. Most of the code I’ve seen written by others does not include error handling code. If you want your code to show any errors it encounters, modify your code as below.

Public Function BEFORESAVE() As Boolean
On Error GoTo errCode
 If Not IsNumeric(Trim(ThisForm.m2mpageframe1.page1.txtfordername.Value)) Then
    MsgBox ("The Ref Name field can only contain numbers.")
    BEFORESAVE = False
    ThisForm.m2mpageframe1.page1.txtfordername.SetFocus
    Exit Function
 End If
 BEFORESAVE = True
errCode:
    MsgBox "ERROR: " & Err.Number & " " & Err.Description
End Function

As I mentioned above, I'll be including VBA, SQL, and VFP code which you are free to use and modify. Feel free to comment if there are questions or suggestions for improvement.

Related posts:

1 comment to First learn stand, then learn fly. Nature rule, Daniel-san, not mine. (VBA)

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>