Array Type Mismatch

S

State Troopers

Hi.
I am having a problem with one of my functions.

here is the code:


strArray(i) = TextBoxGetLines(txtDone, False)

-------


Function TextBoxGetLines(tb As TextBox, Optional KeepHardLineBreaks As
Boolean) As String()

TextBoxGetLines = result()

End Function




Am I passing the variables into the function correctly?

Thanks.
-State
 
M

Michel Walsh

Hi,


Your function return an array of string and you try to fit this array inside
a container devised to hold just a single string (I assume strArray is dim
as String(), not as Object() ).

=======================
Public Sub Void()
Dim array1(1 To 2) As String
Dim array2() As String
Dim array3(1 To 2) As Variant
Dim array4() As String

array2 = Split("aaa bbb ccc")
array3(1) = array2
array4 = array2
array1(1) = array2


End Sub
========================


the last executable line produces your mismatch, comment it and every thing
is fine.

You can:
assign an array of string to a redim-ensionable array of string
(array2 = split(...) )
assign an array of string to a variant or an object container
(array3(1) = array2)
assign an array of string to a redim-ensionable array of string (
array4=array2)

You cannot:
assign an array to a non-variant non-object container
assign an array to a non-redim-ensionable array: array3=array1
would fail, even if both are dimmed as array (1 to 2) and even if a variant
can hold a string.


That is assuming you use VBA6. Previous versions (Access 97 and older) don't
necessary play the same game, and don't have built-in functions like split.


Hoping it may help,
Vanderghast, Access MVP
 
S

State Troopers

Hi.

I tried commenting out:
TextBoxGetLines = result()

and I still recieve the type mismatch error.

strArray is pre-determined ...strArray(6) as string

Am I passing the variables correctly,
strArray(i) = TextBoxGetLines(txtDone, False)

From what I can tell the function requires a "textbox" name, and a true/false.
Function TextBoxGetLines(tb As TextBox, Optional KeepHardLineBreaks As
Boolean) As String()


....and the loop for the array

For i = 0 To UBound(strArray)
strArray(i) = TextBoxGetLines(txtDone, False)
Debug.Print i & " " & strArray(i)
Next


I have no idea why I am getting type mismatch.

your help is appreciated.

Thanks.
-State
 
K

Klatuu

Not really enough here to really see the problem.
What error are you getting?
What does result() do?

I see you are passing a textbox object to TextBoxGetLines. First, it is
always best to fully qualify your object references. That way Access is less
likely to get confused. If txtDone is a control on your main form, I would
suggest you use Me.txtDone.
 
S

State Troopers

Heres the function:

-------------------------------------FUNCTION-------------------------------------
Function TextBoxGetLines(txtDone As TextBox, Optional KeepHardLineBreaks As
Boolean) As String()
Dim result() As String
Dim i As Long

' Activate soft line breaks. A soft line break is marked by the
' CR-Cr-LF sequence.
SendMessage txtDone.hwnd, EM_FMTLINES, True, ByVal 0&

' Retrieve all the lines in one operation and split results.
' This operation will leave trailing CR character for soft line breaks
only.
result() = Split(txtDone.Text, vbCrLf)

' We need a loop to trim the trailing CR character. If the second
' argument is true, we need to manually add a CR-LF pair to all
' the lines that don't contain such trailing CR char.
For i = 0 To UBound(result)
If Right$(result(i), 1) = vbCr Then
result(i) = Left$(result(i), Len(result(i)) - 1)
ElseIf KeepHardLineBreaks Then
result(i) = result(i) & vbCrLf
End If
Next

' Deactivate soft line breaks.
SendMessage txtDone.hwnd, EM_FMTLINES, False, ByVal 0&

TextBoxGetLines = result()
End Function


------------------------------------MAIN
CODE----------------------------------------
Dim strH As String
Dim strC As String
Dim strArray(6) As String
Dim i As Integer

txtInput.SetFocus
strH = txtInput.Text


strC = stripHTML(strH)

txtDone.SetFocus
txtDone.Text = strC

For i = 0 To UBound(strArray)
strArray(i) = TextBoxGetLines(txtDone, False)
Debug.Print i & " " & strArray(i)
Next


----------------------------------------EXPLANATION------------------------------------

The process is: I grab the source code from a website, then stip it of all
html tags. I am then left with 6 lines. the first is a blank line, and then 5
lines of text...this is for EVERY case.
I want to take those 6 lines, and store them into an array. From there, I
will validate whether the element contains text <> "" ....and then store the
elements in a table.

So far, I have everything except the storing of the elements in the array.

Thanks.
 
M

Michel Walsh

Hi,


You cannot assign a predetermined Array, even if it has the right
dimensions. You can only assign (use on the left side of the equal sign) an
un-determined (redim -able) array.

When you comment out (the valid line)

TextBoxGetLine = result()


which indicated which result is to be returned by your function, your
function STILL RETURNS an array, because of its header (signature):



Public Function TextBoxGetLine( ....arguments... ) As String()



note the As String(). It should be As String without parenthesis, to
return just ONE single string, not an array of string.

When a function is meet, VBA creates a variable with the name of the
function. Here, VBA creates a variable TextBoxGetLine As String(). That
variable is an array of string, and CAN accept the instruction

TextBoxGetLine = result()


since it is redim-able. If you comment it out, you just get a redim-able
array as result. The problem is not there, but it is when you try to
capture the result in your ***CALLING*** code:


myVariable(index) = TextBoxGetLine( ... )


So, either remove the () in the function signature (AND return JUST ONE
STRING), either keep your function as it is, and make your variable a
redim-able variable, in your CALLING code (and do NOT use a index):

myRedimable = TextBoxGetLine( ... )

and not

myRedimable(index) = TextBoxGetLine( ... )


either, finally, make your variable, in you calling code, as VARIANT rather
than as STRING (but that means you just move the problem to somewhere else
in your code, I think).


Hoping it may help,
Vanderghast, Access MVP
 
M

Michel Walsh

Hi,


From your post to Klatuu, it seems you need a variant, after all. In your
***calling*** code, something like:


Dim strArray() As VARIANT ' not As STRING !!!
Dim sixLines() As String ' keep it Redimable !!! even if you now it is 6
lines
Dim singleLine As String
...
strArray(messageNumber) = TextBoxGetLines(... ) ' get the whole bunch
of lines
' and store them as 1 message, one "block"
...
sixLines = strArray(whichMessage) ' retrieve the block of six lines
singleLine = sixLines(0) ' retrieve the first line of that block
singleLine = strArray(whichMessage)(0) ' does the same, in one line of
code.
...


Hoping it may help,
Vanderghast, Access MVP
 
K

Klatuu

It appears you are trying to use string data types where parameter arrays
are needed. Look in VBA Help and read up on Parameter Array. I think that
might give you some ideas on how to make your code work.
 
S

State Troopers

It seems that the function runs smoothly now, and that your method works.

BUT

Nothing is stored in the array.

Heres the fuction:

=====


Function TextBoxGetLines(txb As TextBox, Optional KeepHardLineBreaks As
Boolean) As String

Dim result() As String
Dim i As Long

' Activate soft line breaks. A soft line break is marked by the
' CR-Cr-LF sequence.
'SendMessage txb.hwnd, EM_FMTLINES, True, ByVal 0&

' Retrieve all the lines in one operation and split results.
' This operation will leave trailing CR character for soft line breaks
only.
result() = Split(txb.Text, vbCrLf)

' We need a loop to trim the trailing CR character. If the second
' argument is true, we need to manually add a CR-LF pair to all
' the lines that don't contain such trailing CR char.
For i = 0 To UBound(result)
If Right$(result(i), 1) = vbCr Then
result(i) = Left$(result(i), Len(result(i)) - 1)
TextBoxGetLines = result(i)
ElseIf KeepHardLineBreaks Then
result(i) = result(i) & vbCrLf
TextBoxGetLines = result(i)
End If
Next

' Deactivate soft line breaks.
'SendMessage txb.hwnd, EM_FMTLINES, False, ByVal 0&



End Function



=====CODE======


Dim strArray() As Variant
Dim sixLines() As String
Dim singleLine As String
Dim i As Integer

txtInput.SetFocus
strH = txtInput.Text


ReDim strArray(6)
ReDim sixLines(6)
strC = stripHTML(strH)

txtDone.SetFocus
txtDone.Text = strC

For i = 0 To UBound(strArray)
strArray(i) = TextBoxGetLines(Me.txtDone, False)
sixLines(i) = strArray(i)
singleLine = sixLines(i)

Debug.Print i & " " & singleLine
Next




=========END OF CODE===========


I am still somewhat confused as to why nothing is stored in the array.
Will keep trying.

Thanks
 
S

State Troopers

I got the datatypes to work with eachother.

The function is now the problem.
I am getting "" for all the elements of my array.

thanks again.
-State
 
S

State Troopers

Somebody needs to smack me.
Always thinking complex.

Erase the function...code...ALL OF IT

replace with:

aVar() = Split(strC, vbCrLf)
For i = 0 To UBound(aVar())
Debug.Print i & " " & aVar(i)
Next


simple as that.

Thanks for the help Klatuu and Mr. Walsh.

Cheers
-State
 
M

Michel Walsh

Hi,


the proposed modifications were exclusive (ie, apply one, not the 3).

So,

keep the () at the signature:


Function TextBoxGetLines(txb As TextBox, Optional KeepHardLineBreaks As
Boolean) As String()


inside this function, add the line

TextBoxGetLines=result

just before leaving it (and remove the 2 lines TextBoxGetLines =
sult(i) )
so that the function will return 6 strings in one array.



In your calling code, if you have just one "block" of 6 lines, try:


sixLines = TextBoxGetLines(Me.txtDone, False)
For i = 0 To UBound(strArray)
singleLine = sixLines(i)
Debug.Print i, singleLine
Next i




Vanderghast, Access MVP
 
Top