Left function - 'Expected Array'

Clive Beaton
Clive Beaton used Ask the Experts™
on
Can anyone help me with this compiler error?  I use the Left function elsewhere in a different module which doesn't raise this error.

Thanks in advance
Capture.JPG
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try to use Left$ instead of Left
mbizupNerd
Most Valuable Expert 2012
Top Expert 2013

Commented:
Left$ is equivalent to Left (but obsolete)

Your syntax for Left is fine.

You may want to Dim s1 and s2 on seperate lines of code.  I've seen other posts around here saying that despite the 'As String', the second string will actually get defined as a Variant.

    Dim s1 As String
    Dim s2 As String
    
    s2 = "abc"
    s1 = Left(s2, 1)
    MsgBox s1

Open in new window


That in itself, however shouldnt cause this error.  Your code looks good and works fine for me.

In the VBA editor, check tools --> references.  If any are labeled as MISSING, uncheck them and replace them with the correct version.
Clive BeatonAccess Developer

Author

Commented:
Done that.  Different error.
Capture.JPG
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Clive BeatonAccess Developer

Author

Commented:
mbizup,

Did a Compact & Repair.  Changed back to Left.  Separated the Dim statements.  Still got 'Expected array'

No missing references.

Tried it in an earlier version of the program and it worked fine.  The references were identical as well.

Where from here, I wonder.
mbizupNerd
Most Valuable Expert 2012
Top Expert 2013

Commented:
Is s2 defined anywhere else in this module?

Have you posted your full and complete code, or have you abbreviated it for the purpose of making a concise forum post?  (We'd need to see the full code)
Nerd
Most Valuable Expert 2012
Top Expert 2013
Commented:
Give it a try with different variable names.  Do you get the same results?

And double-checking that you havent use LEFT as a name for one of your own variables, functions, or anything else.

In addition to compacting and repairing, for 'strange' code issues, you can try decompiling.

Before doing so, make a backup and read Jim Dettman's article here:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_2043-Decompile-What-it-is-what-it-does-and-how-to-use-it.html
Clive BeatonAccess Developer

Author

Commented:
S2 only appears in commented out code.

Full code attached.
Left-Code.txt
The code compiled OK on my side. Please check the references in your project. If you have a wrong reference (look for "Missing" word on checked references) this is the behaviour
mbizupNerd
Most Valuable Expert 2012
Top Expert 2013

Commented:
Try decompiling, and also give this a try:

Public Sub ConvertPostcodes()

   Dim rs As Recordset
   Dim i As Long
   Dim s1 As String
   Dim s2 As String
   Dim Mystr As String

   Set rs = CurrentDb.OpenRecordset("tblPostcodes", dbOpenDynaset)

   s2 = ""
   With rs
      If Not .EOF Then
         .MoveFirst
         Do Until .EOF
            s1 = !Locality
            Do Until Len(s1) = 0
              s2 = s2 & Left(s1, 1)
               s1 = Mid(s1, 2)
               i = InStr(s1, " ")
               If i = 0 Then
                  s2 = s2 & s1
               Else
                  s2 = s2 & Mid(s1, i - 1)
                  s1 = Mid(s1, i + 1)
               End If
            Loop
            Debug.Print s2
'            .Edit
'               !Locality = s2
'            .Update
            .MoveNext
         Loop
      End If
   End With
End Sub 

Open in new window


Can you describe what you are trying to do?  there may be a better approach.
Clive BeatonAccess Developer

Author

Commented:
Tried different variable names.

Haven't used Left as a variable or global name.

Read Jim Dettmans article.

Decompiled.

Imported everything into a brand new database.

Created a new module, copied all the code into it (ctl/C Ctl/V), deleted the old module. and compiled.

Still get same error.

Interesting thing is, Left compiles and works in other modules, so I moved the function to another module and it compiled.

So at least I can move on.  Your help has been worth the points.  It put me on the right track.  

I'm going it have to re-visit this because it worries me, so more questions on this will be coming soon..
Clive BeatonAccess Developer

Author

Commented:
See my final comments.
Clive BeatonAccess Developer

Author

Commented:
In answer to your question about what I want to do is take a name like
AUSTRALIAN NATIONAL UNIVERSITY
and convert it into
Australian National University.
I ran into a similar problem recently.

Since this was posted under Access 2010, I am assuming that you are using the function in VBA.


When you have a lot of references... sometimes you inadvertently pull in another reference to "left" or "right" and your appliction can get confused.   In this case if you fully qualify your function call... it will help your application understand which function it should use.

All of the following are equivalent... and one of the more fully qualified calls might work for you.
Sub test()
    
    y = "Test"

    x = Left(y, 1)
    x = Strings.Left(y, 1)
    x = VBA.Strings.Left(y, 1)
    
End Sub

Open in new window


looking through VBA's object browser will help you determine what the "fully qualifed" names are that you should use for any given function.
mbizupNerd
Most Valuable Expert 2012
Top Expert 2013

Commented:
Your code could be simplified alot by converting to Proper case like this:

s = "AUSTRALIAN NATIONAL UNIVERSITY"
Newstring =  strconv(s,vbProperCase)
mbizupNerd
Most Valuable Expert 2012
Top Expert 2013

Commented:
>> I'm going it have to re-visit this because it worries me

Agreed... If you can post a sample database (just scrub it of any sensitive data first), lets keep working at it.
Clive BeatonAccess Developer

Author

Commented:
mbizup,

You're worth your weight in gold.  Thanks very much.  I'll be back in touch.

crb
mbizupNerd
Most Valuable Expert 2012
Top Expert 2013

Commented:
If you haven't already, you should try the comment by buttersk at http:#a38007536, and put the vba prefix in front of Left.

However, since Left is a built in VBA function, unless something is slightly amiss in your database, left should work properly without the prefix (it should take priority over Left in other libraries).

Also check one more time for Left defined implicitly or explicitly as a function or variable anywhere within this module of code (or in your database).

Btw - if you use the Request Attention button, a Moderator can reopen this for you.  Its best  to just have solutions that work marked as the answers, since others in the same situation refer to these threads for solutions.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial