Microsoft KB Archive/109941

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 12:25, 21 July 2020 by X010 (talk | contribs) (Text replacement - "&" to "&")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

INF: Sample Function to Build a Repeating String

Article ID: Q109941
Creation Date: 10-JAN-1994
Revision Date: 01-DEC-1996 The information in this article applies to:

  • Microsoft Access versions 1.0, 1.1, 2.0, 7.0, 97

SUMMARY


Moderate: Requires basic macro, coding, and interoperability skills.

This article demonstrates a sample function called RepeatStr() that you can use to build a string with repeating characters.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x or the "Building Applications" manual in Microsoft Access version 2.0

MORE INFORMATION


The RepeatStr() function requires two arguments. These arguments are a String expression and an Integer representing the number of times you want to repeat the string expression. For example, if you supply the String expression ABC and the Integer 3 to the RepeatStr() function, it will return the String:

   ABCABCABC

The RepeatStr() function is similar to the String$() function, except that the String$() function only repeats the first character of the given string expression. To create the RepeatStr() function, type the following code in a new or existing module:

   '******************************************************
   'Declarations Section of Module
   '******************************************************
   Option Explicit

   '======================================================
   ' Purpose: To build a string of replicated characters.
   ' Accepts: A string expression to be replicated.
   '          An integer representing the number of times
   '          the string expression is to be repeated.
   ' Returns: A string.
   '======================================================
   Function RepeatStr(ByVal RepeatCnt As Integer, StringExpr As String)

      Dim RepString As String, i As Integer
      For i = 1 To RepeatCnt
         RepString = RepString & StringExpr
      Next i
      RepeatStr = RepString
   End Function

To test the RepeatStr() function, type the following line in the Debug Window (or the Immediate window in versions 1.x and 2.0), and then press ENTER:

   ? RepeatStr(3,"ABC")

Note that the following line is printed in the Debug window:

   ABCABCABC

You can substitute any integer and any string expression for the arguments in the above example.



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

©1997 Microsoft Corporation. All rights reserved. Legal Notices.


KBCategory: kbusage kbhowto
KBSubcategory: ExrOthr
Additional reference words: 1.00 1.10 2.00 7.00 97 8.00 replicate
function string