Connecting Tech Pros Worldwide Forums | Help | Site Map

VBA Tutorial

Member
 
Join Date: Oct 2009
Posts: 35
#1: 3 Weeks Ago
Does anyone have any insight about what would be some of the best literature on the web today on how to construct syntax in VBA from the start?

What I am looking for is "learn how to tie your shoes" basic literature. The most basic of the basic of the basic.

Most of what I am getting from anywhere is "how to use functions" and how to write them in VBA.

Here is an example:
Expand|Select|Wrap|Line Numbers
  1.   Option Compare Database
  2.  
  3.  
  4. Sub Form_current()
  5. Dim strParentDocName As String
  6.  
  7. On Error Resume Next
  8. strParentDocName = Me.Parent.Name
  9.  
  10. If Err <> 0 Then
  11. GoTo Form_current_exit
  12.  
  13. Else
  14. On Error GoTo Form_Current_err
  15. If Me.Parent.Name = "Work_Order_Details_SubForm" Then
  16. Me.Parent![Tasks_Form].Requery
  17. End If
  18. End If
  19.  
  20. Form_current_exit:
  21. Exit Sub
  22.  
  23. Form_Current_err:
  24. MsgBox Err.Description
  25. Resume Form_current_exit
  26.  
  27. End Sub 
This doesn't teach me anything. I may have a working function but I have no clue why it work or understand the reasons why I must quote " " around Work_Order_Detail_Subform
Expand|Select|Wrap|Line Numbers
  1.  If Me.Parent.Name = "Work_Order_Details_SubForm" 
because sometime you don't need to and other time you have to put that line in [ ] but if it is on a page then I must first put Form! but other time maybe not because of a reason or another..... For me this is like a 50 years + of experience contractor would get started at showing a 5 year old on how to build a house without showing the kid why he should use a saw and not a shovel to cut wood to start with.

I really don't want to learn what different function do and where they can be used, I want to learn why I would use a function instead of another and why do I have to bracket some in a syntax and why sometime I must put them in "" instead.

AH! Eureka - I am looking for the "CONVENTION" on how to write VBA

Forgive me English is not my first language, I probably do not make much sense to a lot of people.

Newbie
 
Join Date: Oct 2009
Posts: 18
#2: 3 Weeks Ago

re: VBA Tutorial


dear,

search on the web for " free ebook pdf vba dummy".
I have found several sites where you can download these books for free !!


br,
Dököll's Avatar
Moderator
 
Join Date: Nov 2006
Location: Upstate NY - US
Posts: 2,268
#3: 3 Weeks Ago

re: VBA Tutorial


You actually came to the right place, do stay tuned... also good job posting an example. Do tell us what you are after, what you hope to achieve with much effort, and we'll try to steer you in the right direction.

I would also search the VBA forum intensively, just to see what's there.

L8ter!
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,875
#4: 3 Weeks Ago

re: VBA Tutorial


You will find a couple of articles in our Insights section which may help. Here are a couple to get you started.

VBA Event Driven Programming

Access Built in Functions

Macro's or VBA

Also if you have any experience with macros you can create some macros and then convert them to VBA. This will allow you to see how the code translates.

If you have any specific questions please feel free to ask them. That's why we're here.

Mary
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,875
#5: 3 Weeks Ago

re: VBA Tutorial


To get you started with some basic conventions in VBA.

All strings (Lines of text) have to be surrounded by double quotes. However, you should note that any substrings have single quotes. By this I mean if you had a query like:

SELECT CustomerName FROM Customers WHERE City= "New York"

and you had to put that in a string in VBA to pass it to a function then you would have to write it as follows:

"SELECT CustomerName FROM Customers WHERE City= 'New York' "

I hope this makes sense.

Another convention you should be aware of is the fact that all dates must be surrounded by hash marks as follows:

#04/09/2009#

Mary
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,722
#6: 2 Weeks Ago

re: VBA Tutorial


Someone posted Microsoft Access Tutorial recently. I haven't been through it in detail, but I had a quick look and it seemed pretty good. I decided to save it for re-use whenever anyone (like yourself) asked for something like this.

A couple of links that clarify what Mary was talking about (Quotes (') and Double-Quotes (") - Where and When to use them & Literal DateTimes and Their Delimiters (#)). Technically (not necessary, but is good practice if you ever want to move on to other RDBMSs) any string literal used by SQL should be enclosed in single-quotes (').

Good luck with your learning.
Delerna's Avatar
Expert
 
Join Date: Jan 2008
Location: Sydney
Posts: 788
#7: 2 Weeks Ago

re: VBA Tutorial


And just to answer the two specific questions that I see in your post.

Expand|Select|Wrap|Line Numbers
  1.  If Me.Parent.Name = "Work_Order_Details_SubForm"  
  2.  
here you are comparing the content of an objects property (Me.Parent.Name) against a string value, to control whether a piece of code should get executed or not.

The object is (Me.Parent) and the property is (Name).
The name property is a string therefore what you are comparing it with
must also be a string.

Speaking in the context of your question
In VBA, text enclosed within "" is a string
Text not enclosed within "" is a variable

So your code can also be written like this

Expand|Select|Wrap|Line Numbers
  1.  Dim Work_Order_Details_SubForm as string
  2.  Work_Order_Details_SubForm ="Work_Order_Details_SubForm"
  3.  If Me.Parent.Name =   Work_Order_Details_SubForm THEN
  4.  
Here Work_Order_Details_SubForm is a variable for storing strings.

Notice there is no "". It is comparing the content of one object against the content of another object, whereas your code is comparing the content of an object directly against an actual string and therefore needs the "" for the VBA interpretor to recognise it as such.

Without the "" the VBA interpretor would think it was a variable
Delerna's Avatar
Expert
 
Join Date: Jan 2008
Location: Sydney
Posts: 788
#8: 2 Weeks Ago

re: VBA Tutorial


Now for the square brackets.
This one is a bit more difficult as there are variations to the rule.

It is generally used in conjuction with the name of an object.
For example, the name of a field in an sql query
Expand|Select|Wrap|Line Numbers
  1.    SELECT FieldName FROM TheTable
  2.  
Notice there are no square brackets around anything.
If however my personal naming convention separated words with spaces instead of capitalized first characters.


Expand|Select|Wrap|Line Numbers
  1.    SELECT Field Name FROM The Table
  2.  
This would not work because Field and Name would be considered as two fields and there is no comma between them. The query would also look for a table called The.

Table would be considered as erroneous....incomprehensible nonsense.
You must write the query then, like this
Expand|Select|Wrap|Line Numbers
  1.    SELECT [Field Name] FROM [The Table]
  2.  
The square brackets forces the two words, Field Name, to be read as one field.
Get it?
As mentioned, there are variations to the use of [] but they all exist for similar reasons.

Incidentally, this
Expand|Select|Wrap|Line Numbers
  1.    SELECT [FieldName] FROM [TheTable]
  2.  
is not wrong, just unnecessary to include the [] here and therefore, that is the reason they are sometimes there and sometimes not.



I hope that helps a little. Spelling all this out in a tutorial can make for incredibly boring reading and in reality is probably better gained through experience and asking (specific) questions. It is a long road but the journey is very satisfying even if sometimes frustrating.
At least... it is for me.


EDIT
My anti tutorial comment is directed more towards things like
when to use [] and when not to.
It is not intended as a slurr against "VBA Basics" tutorials
Reply