Multiselect rows in listbox based on string - ms-access

I have a multiselect listbox that has around 60 values, the user can go through and select anything they want I can successfully read everything selected and output it as one line like this "1,2,3,4,5" and store that value. The problem I currently have is when the user needs to edit what was selected. How can I reselect the listbox items based on the string "1,2,3,4,5" that was stored earlier?

I figured it out after a lot of searching, here is the code
Dim strValue As String
Dim strArray() As String
Dim x As Integer
Dim startRow As Integer
strValue = rsPrints("ctype")'Get string of row values
strArray = Split(strValue, ",", -1, vbTextCompare)'My string required splitting to remove commas and moving to an array
x = 0 'Set array start point to 0
For Q = LBound(strArray) To UBound(strArray) 'Run through array
startRow = strArray(x)
Me.formctype.Selected(startRow) = True
x = x + 1
Next

Related

Convert Column To Field Order

I am importing an excel spreadsheet into access, and requesting the user to input wchich column holds a userid and phone. Now on the access form, they will be string values, like A & R
I am trying to then convert the alpha value to a number value, but when I use this syntax it is not giving appropriate results. Such as the below produces # when I would want it to produce 3 - what is the appropriate way in Access to convert Letters to Column Numbers
Sub Test()
Dim colletter As String
colletter = "C"
Debug.Print Chr(Val(colletter) + 64)
End Sub
You are really close. You are going to want to use the ASC() function which returns the ASCII value of a character. When you subtract 64, it will get you the correct column index.
Sub Test()
Dim colletter As String
colletter = "C"
Debug.Print Asc(colletter) - 64
End Sub
*EDIT: I've added some controls for multiple letters and to make sure that the letters are upper case. This does, however, limit it to only having two letters, meaning column "ZZ" is your last column, but hopefully your user doesn't have more than 702 columns. :)
Sub Test()
Dim colLetter As String
Dim colNumber As Integer
Dim multiplier As Integer
colLetter = "AB"
multiplier = 0
'If there is more than one letter, that means it's gone through the whole alphabet
If Len(colLetter) > 1 Then
multiplier = Asc(Left(UCase(colLetter), 1)) - 64
End If
colNumber = (multiplier * 26) + Asc(Right(UCase(colLetter), 1)) - 64
Debug.Print colNumber
End Sub
Here's another solution that allows any number of letters, such as "ZZZZ". As you can tell, it is quite similar to the post by #BobtimusPrime.
Public Function RowLetterToNumber(ByVal RowLetter As String) As Integer
If Len(RowLetter) > 1 Then
RowLetterToNumber = RowLetterToNumber(Mid(RowLetter, 2))
RowLetterToNumber = RowLetterToNumber + 26
Else
RowLetterToNumber = Asc(RowLetter) - 64
End If
End Function
Sorry, but can't you simply use: =COLUMN()

Access For Loop Hide Objects

I'm trying to hide/unhide around 30 objects on my form when the user selects certain values from a dropdown menu. I tried the loop below, however I receive the following error: 'Object doesn't support this property or method.' I have this code running on the 'AfterUpdate' of the dropdown menu object.
Dim VisibleVisitFields() As String
Dim VisibleVisitFieldlist As String
Dim varVisibleVisit As Variant
VisibleVisitFieldlist = "VisitDate_Event,VisitTime_Event,VisitSite_Event,VisitStaff_Event,VisitMeet_Event"
VisibleVisitFields = Split(VisibleVisitFieldlist, ",")
If (EventType = 3) Then
For Each varVisibleVisit In VisibleVisitFields
[Forms]![subFRM_TBL_Event-All in One].Controls(varVisibleVisit).visible = True
Exit For
Next
Else
If (EventType <> 3) Then
For Each varVisibleVisit In VisibleVisitFields
[Forms]![subFRM_TBL_Event-All in One].Controls(varVisibleVisit).visible = False
Exit For
Next
End If
End If
Which line triggers the error? Suspect it is reference to the subform that is flawed. Never seen code like that to loop through an array. Suggest naming subform container different from the object it holds, such as ctrEvent. What is EventType - a textbox/field on the form? Consider code:
Dim aryFields As Variant
Dim x As Integer
aryFields = Split("VisitDate_Event,VisitTime_Event,VisitSite_Event,VisitStaff_Event,VisitMeet_Event", ",")
For x = 0 To UBound(aryFields)
Me.ctrEvent.Form.Controls(aryFields(x)).Visible = Me.EventType = 3
Next
Alternative methods not using array:
Set control Tag property then code loops through all controls on form and sets visibility for those that have particular value in Tag.
Dim ctrl As Control
For Each ctrl in Me.ctrEvent.Form.Controls
If ctrl.Tag = "something" Then ctrl.Visibility = Me.EventType = 3
Next
Another is to give controls similar names, like: Visit1, Visit2, etc. Then code:
Dim x As Integer
For x = 1 to 30
Me.ctrEvent.Form.Controls("Visit" & x).Visible = Me.EventType = 3
Next
Advise no spaces or punctuation/special characters (underscore only exception) in naming convention.
You are trying to iterate over an array of strings.
Dim VisibleVisitFields() As String
You need to declare the array to contain variants (which can still contain strings)
Dim VisibleVisitFields() As Variant

How to add array values into dropdown list using vba

i'm splitting the string ;#WR_1;#WR_2;#WR_3;#WR_4;# with VBA code
Sub Splitfn()
Dim str As String
Dim var As Variant
Dim i As Long
str = ";#WR_1;#WR_2;#WR_3;#WR_4;#"
var = Split(str, ";#")
For i = 0 To UBound(var)
Debug.Print i, var(i)
Next i
End Sub
That returns
0
1 WR_1
2 WR_2
3 WR_3
4 WR_4
5
I want to add these values(WR_1,WR_2,WR_3,WR_4) to dropdown list. How to specify that in VBA code
Combo boxes accept strings as a value list:
sStr = ";#WR_1;#WR_2;#WR_3;#WR_4;#"
'' Var = Split(Str, ";#")
sStr = Replace(sStr, "#", "")
Me.Combo9.RowSourceType = "value list"
Me.Combo9.RowSource = sStr
The first value will be empty, because the first character is the delimiter ;, if this is not required, it is easy to change:
sStr=Mid(sStr,2)
Me.Combo9.RowSource = sStr

DataReader with Multiple Row

I'm trying to use the DataReader to grab data from a MS ACCESS Database and then using the items in the datareader to do some calculations. This is what I'm doing with one row.
Dim dataRead As OleDb.OleDbDataReader = dataCom1.ExecuteReader
If (dataRead.Read() = True) Then
x = dataReader.Item("x")
y = dataReader.Item("y")
z = dataReader.Item("z")
End If
I'm trying to do the same with multiple rows that are retrieved at random. At the moment I have this:
Dim dataRead2 As OleDb.OleDbDataReader = dataCom2.ExecuteReader
For i As Integer = 0 To 4
If (dataRead2.Read() = True) Then
a(i) = dataRead2.Item("z")
b(i) = dataRead2.Item("y")
c(i) = dataRead2.Item("x")
i = i + 1
End If
Next
Whenever I run it I get an error.
I get the error "Object reference not set to an instance of an object." on the line a(i)"
It appears your array is declared, but not with any specific dimensions. You should declare it as:
Dim Descarray(4) As String
Which will hold 5 strings (0...4)
You can also use:
ReDim Descarray(5)
After the array has already been declared.

extract numbers from string in access

I need help creating a VB code or expression in Access 2010 that will group numbers from a string where each set starts with number 6 and is always 9 characters long.
Example of strings:
Order Confirmation # 638917872-001 Partial Order/$23.74 RECEIVED
Order Confirmation - Multiple Orders - Order Confirmation#639069135-001/$297.45 - Order Confirmation#639069611-001/$32.08.
I'm using a VB code to remove all the alpha characters but that just leaves me with:
6389178720012374 from string 1 and
639069135001297456390696110013208 from string 2.
All I care about is the order number that starts with 6 and is 9 characters long. Any help would be greatly appreciated, I know there's an easier way.
VB.NET Solution:
If you just need the first 9 numbers from your resulting strings you could use String.Substring, ie:
Dim numberString as String = "6389178720012374"
Dim newString As String = numberString.Substring(0, 9)
MessageBox.Show(newString)
shows 638917872
MSDN Link
EDIT:
Maybe you would want to use a RegEx - something like this perhaps can get you started:
Private Sub Input()
Dim numberString As String = "Order Confirmation # 638917872-001 Partial Order/$23.74 RECEIVED"
Dim numberString2 As String = "Order Confirmation - Multiple Orders - Order Confirmation#639069135-001/$297.45 - Order Confirmation#639069611-001/$32.08"
GiveMeTheNumbers(numberString)
GiveMeTheNumbers(numberString2)
End Sub
Function GiveMeTheNumbers(ByVal s As String) As String
Dim m As Match = Regex.Match(s, "6\d{8}") 'get 9 digit #s begin w/6
Do While m.Success
MessageBox.Show(m.Value.ToString)
m = m.NextMatch()
Loop
Return False
End Function
Results -
MessageBox1: 638917872
MessageBox2: 639069135
MessageBox3: 639069611
You can use this function ... tested in VB.NET
Function NumOnly(ByVal s As String) As String
sRes = ""
For x As Integer = 0 To s.Length - 1
If IsNumeric(s.Substring(x, 1)) Then sRes = sRes & s.Substring(x, 1)
Next
return sRes
End Function
Little modif for ms-access
OK, here's a VBA solution. You'll need to add Microsoft VBScript Regular Expressions to your references.
This will match every 9 digit number it finds and return an array of strings with the order #s.
Function GetOrderNum(S As String) As String()
Dim oMatches As Object
Dim aMatches() As String
Dim I As Integer
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
ReDim aMatches(0)
RE.Pattern = "\d{9}"
RE.Global = True
RE.IgnoreCase = True
Set oMatches = RE.Execute(S)
If oMatches.Count <> 0 Then
ReDim aMatches(oMatches.Count)
For I = 0 To oMatches.Count - 1
aMatches(I) = oMatches(I)
Next I
End If
GetOrderNum = aMatches
End Function