Wrox Programmer Forums
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 8th, 2006, 01:59 AM
Registered User
Join Date: Jan 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to hiruni Send a message via MSN to hiruni
Default creating array

i want to creat an array in vb6 for storing asc values of a given word. how can i creat that array..a one asc value must store in as array element.pls help me for tht. thanx

Old February 8th, 2006, 10:05 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi

hi there..

if you dont know how many items will have the array, just do it with a collection.

if you still want to use an array, just dim it like:

dim MyArray(10) as integer


Old February 8th, 2006, 12:37 PM
Registered User
Join Date: Jan 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts

If what you are doing does not require high performance,
IE: writing a game...

Try this:

Dim MyArray() as Integer 'Array to hold the Acii values
Dim intLoop as integer 'For loop variable
dim intLength as Integer 'Length of the string being converted to
                         'Ascii values

intLength = Len(strMyString) 'Get the length of the string

For intLoop = 0 to (intLength - 1) 'Loop through the string
    Redim Preserve MyArray(intLoop) 'Redimension the array and
                                    'preserve the existing elements
    'Take the character in the intLoop + 1 position and
    'get its Ascii value.
    MyArray(intLoop) = Asc(Mid(strMyString, intLoop + 1, 1))

Old February 8th, 2006, 03:26 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts

to AndreK: no need to "redim preserve" in every loop: just declare the array before the loop as:

redim MyArray(Len(strMyString)-1)

Old February 8th, 2006, 03:32 PM
Registered User
Join Date: Jan 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts

Good catch...

Old February 9th, 2006, 03:22 AM
Friend of Wrox
Join Date: Dec 2005
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nalla Send a message via Yahoo to nalla

Hi AndreK,

I tried your code but it didn't make my array elements in ascending order.

   I wrote a small function to do this. You have to pass the string you need to make in ascening order and function will return a array of elements ordered by ASC. If you are worrying about the CASE of your characters please modify the relavant line.
   If you don't have SQL Server use Access or any other ...

Public Function String_Order(ByVal strMyString As String) As Variant

Dim MyArray() As String
Dim intLoop As Integer
Dim intVal As Integer
Dim rsPrimary As New ADODB.Recordset


cnn.Execute "CREATE TABLE #values(value int)"

For intLoop = 0 To (Len(strMyString) - 1)
    ReDim Preserve MyArray(intLoop)
    intVal = Asc(UCase(Mid(strMyString, intLoop + 1, 1)))
    cnn.Execute "INSERT INTO #values VALUES(" & intVal & ")"

rsPrimary.Open "SELECT value FROM #values ORDER BY value", cnn
intLoop = 0
Do While Not rsPrimary.EOF
    MyArray(intLoop) = Chr(CLng(rsPrimary!Value))
    intLoop = intLoop + 1

String_Order = MyArray()

Exit Function


    cnn.Execute "DROP TABLE #values"

End Function

Private Sub Form_Load()
    Set cnn = New ADODB.Connection
    cnn.Open "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=serverdatabase;Data Source=(local)"
End Sub

[u]Testing the Function</u>
Private Sub Command1_Click()

    Dim arrString As Variant
    Dim str As String
    Dim i As Integer

    arrString = String_Order("Nalaka")

    For i = 0 To UBound(arrString)
        str = str & arrString(i)
    Next i

    MsgBox str

End Sub

Please post if you need more info regarding this.

nalaka hewage

Similar Threads
Thread Thread Starter Forum Replies Last Post
creating an array of size 2^16 bits scoobie C++ Programming 2 November 9th, 2005 08:59 PM
Dynamically creating an array youngj PHP How-To 0 November 8th, 2004 03:42 AM
trouble creating array Tachyon Beginning PHP 3 June 28th, 2004 11:25 AM
Creating an array of image Dinesh22 VB.NET 2002/2003 Basics 1 January 14th, 2004 09:56 PM
Creating an associatve array from a string Walter G PHP How-To 5 August 19th, 2003 02:59 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.