Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a problem that I have been trying to solve for a couple of days with MS Access and a VBA function.

To start I have a table as follows

Name = Team

fields are mostly text bases, unless stated otherwise ID(autonumber primary key) Non Prenom Location TeamID (created by concatenting the Nom, Prenom and Location fields)

On my form I would like to extract the partial details entered into the Nom, Prenom, and Location fields. Then pass this back to the Database to enter into the TeamID field for the individual.

The extraction should take the form of TeamID = mid(Location,0,4) & mid(Prenom,0,2) & mid(Nom,0,2à)

However I realise that I can't put this into the 'controle source' section of the properties for any field.

So after much searching I decided that I should use a function in a separate module (I do this concatenating quite often for creation of 'sensible' index values on a large number of my tables, I find the autonumber primary key not very user friendly, or self explanatory.

So in my database file I created a module (called getInfo) with the public function

Public Function getID() As String
Dim r As String
Dim i As String
i = "Twenty"

Below are some of the options I have tried....

'r = VBA.Strings.UCase$(String:=i) 'turning the value of i to uppercase

getID = r 'returns "TWENTY" as expected

or

'r = VBA.Strings.Mid$("TWENTY", 0, 2)
getID = r 'error and highlights above line?

or

'r = StrReverse(i)
getID= r 'again error and highlights above line

getID = VBA.Strings.Mid$(String:="TWENTY", Start:=0, Length:=2)
End Function

I then opent eh 'execution' window and run the function.

I seem to be only able to convert the value to upper or lower case, any searching or manipulation of the string just gives me a message bow with the following error

Execution Error '5' argument or procedure call incorrect

(please bear with me I am working on a french terminal and so my translation of this error may not be very acurate).

However all the functions come up correctly when I type them, as do the parameters being passed. when I search the net the info is also the same http://www.techonthenet.com/excel/formulas/index_vba.php

So I am lost as to where I am going wrong.

I guess I am not declaring the value of my variables correctly, but I can't see why. This code doesn't work elsewhere (other terminals) either, so I'm sure it must be my error!

But what is it.

I feel that this is a reallybasic problem, but just can't get the function to work when manipulating a string.

Do I need to call this function from a sub only, and not directly, will this be the same on my form page?

I can create a minidatabase with just this code in if required.

share|improve this question
1  
What's this VBA.Strings stuff. Granted my VBA has been in Word and Excel but there you simply put in something like getID = mid(i,1,2). –  paxdiablo Oct 11 '11 at 14:13
1  
Your having a giggle right, Strings aren't Zero indexed! I knew it would be something stupid. And the VBA.Strings stuff was me trying to debug as to why it wasn't working and so used fully qualified namespace (just in case!) –  DaveM Oct 11 '11 at 14:25

1 Answer 1

up vote 4 down vote accepted

You should not need the VBA.String namespace prefix, and the ArgName:= syntax is optional so long as you follow same-order rules for optional paramaters.

Mid$("TWENTY", 0, 2) fails because in VBA strings start at index 1, so Mid$("TWENTY", 1, 2) would return TW

share|improve this answer
    
How did I miss that, why didn't I find it sooner! –  DaveM Oct 11 '11 at 14:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.