Convert Column To Field Order - ms-access

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()

Related

Extract char/numbers using Mid and Pos function

I have a string with a value of "String_test|123456"
How can I extract the numbers and characters and put them to another variable
string2 = "String_test" and int = 123456 using Mid / Pos functions.
Thanks!
Assuming you want a PowerBuilder answer rather than the ones given...
You seem to have a string with a 'string' portion and a 'number' portion delimited by a pipe character '|'. Assuming this is the normal format of the data you find the position of the pipe by:
li_pipepos = Pos(ls_string, '|')
Then the string portion is found thusly:
ls_string_portion = Mid(ls_string, 1, li_pipepos - 1)
The number portion is found:
ls_number_portion = Mid(ls_string, li_pipepos + 1 )
Then you convert the number portion into an integer (watch out since in PB an integer is not very large - i'd use a long instead) by:
ll_number = Long(ls_number_portion)
Now if your data isn't in a standardized format you will need to loop through all the characters to determine if they are a number or not and then append them to a string variable (one for numbers and another for characters) then finally convert the number string into a number.
Assuming | is the delimiter, it's easier to use Split function to do this.
Sub Test()
Const sampleStr As String = "String_test|123456"
Dim splitArr() As String
splitArr = Split(sampleStr, "|")
Dim string2 As String
string2 = splitArr(0)
Debug.Print string2 'String_test
Dim int2 As Long
int2 = splitArr(1)
Debug.Print int2 '123456
End Sub
You can modify this user defined function to suit your needs.
Public Function ReturnIntegers(cell As Range) As String
Dim stringholder As String
stringholder = cell.value
Dim pos As Integer
pos = InStr(1, stringholder, "|", vbTextCompare)
ReturnIntegers = Right(stringholder, (Len(stringholder) - pos))
End Function

How to populate a dynamic array in MS Access?

I have a dynamic array that I want to append values to. The number of values to be appended is not fixed
I was trying to do something like this:
Dim array() As Integer
ReDim Preserve array(UBound(array)+1)
bulkJob(UBound(array) + 1) = Me.ID
I get subscript out of range error at ReDim Preserve array(UBound(array)+1). Is there a way to do this?
Not quite clear what you are trying to do, but this could get you some ideas:
Public Function BuildJobs(Id As Integer)
Static bulkJob() As Integer
Dim Upper As Integer
On Error Resume Next
Upper = UBound(bulkJob) + 1
On Error GoTo 0
ReDim Preserve bulkJob(Upper)
' Fill in value.
bulkJob(Upper) = Id
' Do something.
Debug.Print UBound(bulkJob), bulkJob(Upper)
End Function
"Restart" the array like this:
ReDim bulkJob(0)
bulkJob(0) = 0

MS access Query pull 9 digit number from string

I've been looking on google but cant quite find exactly what i need & using these sorts of functions is my weak spot.
Basically, from an alphanumeric string in a table, I need to pull out any 9 digit numbers. they will always start with 0. The numbers won't have any breaks within them.
I would also prefer to retrieve the 9 digits using a query. ie have the pulled number sit alone in a separate column. there will only be 1 9 digit number
I have the below, which pulls out the number, i just dont know how to limit the length with it as the string with have other things, like parts of addresses and whatnot.
PHONE: Mid([DESCRIPTION],InStrRev([DESCRIPTION]," ",InStr([DESCRIPTION],"0"))+1,InStr(InStr([DESCRIPTION],"0"),[DESCRIPTION]," ")-InStrRev([DESCRIPTION]," ",InStr([DESCRIPTION],"0")))
You can use Split for this:
Public Function StripNine(ByVal Text As String) As String
Dim Words As Variant
Dim Index As Integer
Dim Result As String
Words = Split(Text, " ")
For Index = LBound(Words) To UBound(Words)
If Len(Words(Index)) = 9 Then
If IsNumeric(Words(Index)) Then
Result = Words(Index)
Exit For
End If
End If
Next
StripNine = Result
End Function
This will run and return for an example like this:
s = "This is 1 message 00 with a 09-digit number 087654321 somewhere in the body"
? StripNine(s)
087654321
You can use the Mid() function. Like this:
Mid("Test1String", 1, 4) 'Result = "Test"
Mid("Test2String", 5, 3) 'Result = "2St"
Mid("Test3String", 7, 2) 'Result = "tr"
And if you want to eliminate the blanks you can use:
Trim(" Test4 String ") 'Result = "Test4 String"
Replace(" Test5 String ", " ", "") 'Result = "Test5String"
UPDATE:
Try this:
Dim intPostion As Integer
Dim strResult As String
intPosition = InStr(YourString, 0) 'Search for the 0
strResult = Mid(YourString, intPosition, 9) 'Extract 9 digits
If you put this in a loop (so that intPosition checks the whole string) and check if strResult has only numeric numbers you should get your result.

Multiselect rows in listbox based on string

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

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