|
-
Jun 2nd, 2007, 02:49 PM
#1
Thread Starter
New Member
Search and update data in an array... need code
My application is somewhat database oriented. And here's what i'm trying to do.
A group of users (an unidentified number) is connected to my database program and every few seconds they send me a data string which updates their status in my database. What I initially wanted to do was an array of strings.
So whenever I receive a string, i automatically tokenize it, the first token is the user ID, and search only the first substring of the array of strings, which identifies the user. And then it writes over this string with the new string of data, if it does not find this first substring it will assume i have a new user and add a new element to the array.
So my strings are structured like the following...
(;"userID":XXX:YYYYY:ZZZZZ
Here's a sample of what would happen...
I receive the following string
(;7374648:XXX:YYYY:ZZZZZ
And i want to go through the following array of strings
(;7536472:XXX:YYYY:ZZZZZ
(;7393848:XXX:YYYY:ZZZZZ
(;7392838:XXX:YYYY:ZZZZZ
(;7302933:XXX:YYYY:ZZZZZ
(;7173642:XXX:YYYY:ZZZZZ
(;7374648:XXX:YYYY:ZZZZZ
(;7759404:XXX:YYYY:ZZZZZ
And update the string with the appropriate user ID with the new string that just came in.
Help?
-
Jun 2nd, 2007, 04:33 PM
#2
Re: Search and update data in an array... need code
I'd recommend storing everything in a UDT array. But if you want to work with strings, you can use something like this. Might want to paste it into a new VB project so it's easier to read:
Code:
Option Explicit
Private strUsers() As String
Private Sub Form_Load()
InitStrArray
End Sub
'An example of what you do when you receive the data.
'ie: (;7374648:XXX:YYYY:ZZZZZ
Private Sub DataReceived(ByRef Data As String)
'(;7374648:XXX:YYYY:ZZZZZ
Dim lonIndex As Long, lonID As Long
'Get user ID from data string.
lonID = GetID(Data)
lonIndex = LookupByID(lonID)
If lonIndex > -1 Then
'Update user info.
strUsers(lonIndex) = Data
End If
End Sub
'Does a 'lookup' of a user.
'Returns the index that the user id is on.
'Returns -1 if not found.
Private Function LookupByID(ByVal ID As Long) As Long
'Find ID.
Dim lonLoop As Long
Dim lonID As Long, lonFound As Long
lonFound = -1
For lonLoop = LBound(strUsers()) To UBound(strUsers())
If Len(strUsers(lonLoop)) > 0 Then
lonID = GetID(strUsers(lonLoop))
If lonID = ID Then
lonFound = lonLoop
Exit For
End If
End If
Next lonLoop
LookupByID = lonFound
End Function
'Extracts the ID from a data string.
Private Function GetID(ByRef Data As String) As Long
'(;7374648:XXX:YYYY:ZZZZZ returns: 7374648
Dim intPos As Integer
intPos = InStr(1, Data, ":")
If intPos > 0 Then
GetID = CLng(Mid$(Data, 3, intPos - 3))
End If
End Function
'Load some stuff into the string array for testing.
Private Sub InitStrArray()
ReDim strUsers(5) As String
Dim intLoop As Integer
strUsers(0) = "(;7393848:XXX:YYYY:ZZZZZ"
strUsers(1) = "(;7392838:XXX:YYYY:ZZZZZ"
strUsers(2) = "(;7302933:XXX:YYYY:ZZZZZ"
strUsers(3) = "(;7173642:XXX:YYYY:ZZZZZ"
strUsers(4) = "(;7374648:XXX:YYYY:ZZZZZ"
strUsers(5) = "(;7759404:XXX:YYYY:ZZZZZ"
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Erase strUsers
End Sub
-
Jun 3rd, 2007, 03:53 AM
#3
Re: Search and update data in an array... need code
Why not just have them update a table for this prupose in the database since they're already connected to it?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|