开发者

VBA functions not working, returns error

开发者 https://www.devze.com 2023-04-12 01:01 出处:网络
I have a problem that I have been trying to solve for a couple of days with MS Access and a VBA function.

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 tab开发者_如何转开发les, 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号