Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] - نسخه متنی

Alison Balter

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



Working with Built-In Functions


VBA has a rich and comprehensive function library as well as tools to assist in their use.

Built-In Functions


Some of the more commonly used functions and examples are listed in the following sections. On some rainy day, go through the online Help to become familiar with the rest.

NOTE

The following examples are located in basBuiltIn in the Chap7Ex database.

Format

The Format function formats expressions in the style specified. The first parameter is the expression you want to format; the second is the type of format you want to apply. Here's an example of using the Format function:

Sub FormatData()
Debug.Print Format$(50, "Currency")
'Prints $50.00
Debug.Print Format$(Now, "Short Date")
'Prints the current date
Debug.Print Format$(Now, "DDDD")
'Displays the word for the day
Debug.Print Format$(Now, "DDD")
'Displays 3 - CHAR Day
Debug.Print Format$(Now, "YYYY")
'Displays 4 - digit Year
Debug.Print Format$(Now, "WW")
'Displays the Week Number
End Sub

Instr

The Instr function returns the position where one string begins within another string:

Sub InstrExample()
Debug.Print InStr("Alison Balter", "Balter") 'Returns 8
Debug.Print InStr("Hello", "l") 'Returns 3
Debug.Print InStr("c:\my documents\my file.txt", "\") 'Returns 3
End Sub

InStrRev

InStrRev begins searching at the end of a string and returns the position where one string is found within another string:

Sub InstrRevExample()
Debug.Print InStrRev("c:\my documents\my file.txt", "\") 'Returns 16
End Sub

Notice that the InStr function returns 3 as the starting position for the backslash character within "c:\my documents\my file.txt", whereas the InStrRev function returns 16 as the starting position for the backslash character in the same string. This is because InStr starts searching at the beginning of the string, continuing until it finds a match, whereas InStrRev begins searching at the end of the string, continuing until it finds a match.

Left

Left returns the left-most number of characters in a string:

Sub LeftExample()
Debug.Print Left$("Hello World", 7) 'Prints Hello W
End Sub

Right

Right returns the right-most number of characters in a string:

Sub RightExample()
Debug.Print Right$("Hello World", 7) 'Prints o World
End Sub

Mid

Mid returns a substring of a specified number of characters in a string. This example starts at the fourth character and returns five characters:

Sub MidExample()
Debug.Print Mid$("Hello World", 4, 5) ''Prints lo Wo
End Sub

UCase

UCase returns a string that is all uppercase:

Sub UCaseExample()
Debug.Print UCase$("Hello World") 'Prints HELLO WORLD
End Sub

DatePart

DatePart returns the specified part of a date:

Sub DatePartExample()
Debug.Print DatePart("YYYY", Now)
'Prints the Year
Debug.Print DatePart("M", Now)
'Prints the Month Number
Debug.Print DatePart("Q", Now)
'Prints the Quarter Number
Debug.Print DatePart("Y", Now)
'Prints the Day of the Year
Debug.Print DatePart("WW", Now)
'Prints the Week of the Year
End Sub

DateDiff

DateDiff returns the interval of time between two dates:

Sub DateDiffExample()
Debug.Print DateDiff("d", Now, "12/31/2005")
''Days until 12/31/2005
Debug.Print DateDiff("m", Now, "12/31/2005")
''Months until 12/31/2005
Debug.Print DateDiff("yyyy", Now, "12/31/2005")
''Years until 12/31/2005
Debug.Print DateDiff("q", Now, "12/31/2005")
''Quarters until 12/31/2005
End Sub

DateAdd

DateAdd returns the result of adding or subtracting a specified period of time to a date:

Sub DateAddExample()
Debug.Print DateAdd("d", 3, Now)
'Today plus 3 days
Debug.Print DateAdd("m", 3, Now)
'Today plus 3 months
Debug.Print DateAdd("yyyy", 3, Now)
'Today plus 3 years
Debug.Print DateAdd("q", 3, Now)
'Today plus 3 quarters
Debug.Print DateAdd("ww", 3, Now)
'Today plus 3 weeks
End Sub

Replace

Replace replaces one string with another:

Sub ReplaceExample()
Debug.Print Replace("Say Hello if you want to", "hello", "bye")
'Returns Say Bye if you want to
Debug.Print Replace("This gets rid of all of the spaces", " ", ")
'Returns Thisgetsridofallofthespaces
End Sub

StrRev

StrRev reverses the order of text in a string:

Sub StrReverseExample()
Debug.Print StrReverse("This string looks very funny when reversed!")
'Returns !desrever nehw ynnuf yrev skool gnirts sihT
End Sub

MonthName

MonthName returns the text string associated with a month number:

Sub MonthNameExample()
Debug.Print MonthName(7)
'Returns July
Debug.Print MonthName(11)
'Returns November

Functions Made Easy with the Object Browser


With the Object Browser, you can view members of an ActiveX component's type library. In plain English, the Object Browser enables you to easily browse through a component's methods, properties, and constants. You can also copy information and add it to your code. It even adds a method's parameters for you. The following steps let you browse among the available methods, copy the method you want, and paste it into your code:


  • With the VBE active, select View, Object Browser from the menu (note that the menu line also shows an icon that you can use from the toolbar), or press F2 to open the Object Browser window (see Figure 7.11).

    Figure 7.11. The Object Browser showing all the classes in the Chap7ex database and all the members in the basUtils module.

  • The Object Browser window is divided into two parts; the upper part of the window and the lower part. The drop-down list at the upper-left of the window is used to filter the items to be displayed in the lower part of the window. Use this drop-down list to select the project or library whose classes and members you want to view in the lower part of the window.

  • In the lower portion of the window, select the class from the left list box, which lists Class modules, templates for new objects, standard modules, and modules containing subroutines and functions.

  • Select a related property, method, event, constant, function, or statement from the Members Of list box. In Figure 7.11, the basUtils module is selected from the list box on the left. Notice that the subroutines and functions included in basUtils appear in the list box on the right.

  • Click the Copy to Clipboard button (third from the right in the upper toolbar within the Object Browser window) to copy the function name and its parameters to the Clipboard so that you can easily paste it into your code.


  • The example in Chapters 8 and 22.

    Figure 7.12. The Object Browser with the VBA library selected.



    / 544