Re: Array Type Mismatch
- From: State Troopers <StateTroopers@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 2 May 2006 07:40:02 -0700
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
"Michel Walsh" wrote:
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
"Michel Walsh" <vanderghast@VirusAreFunnierThanSpam> wrote in message
news:eIR3g2ebGHA.1264@xxxxxxxxxxxxxxxxxxxxxxx
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
"State Troopers" <StateTroopers@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:F683963B-198A-4EF9-912F-6163627D27DD@xxxxxxxxxxxxxxxx
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
"Michel Walsh" wrote:
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
"State Troopers" <StateTroopers@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message
news:F88EA92C-26F6-4759-95C9-ABEABAF313FC@xxxxxxxxxxxxxxxx
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
- Follow-Ups:
- Re: Array Type Mismatch
- From: Michel Walsh
- Re: Array Type Mismatch
- References:
- Re: Array Type Mismatch
- From: Michel Walsh
- Re: Array Type Mismatch
- From: State Troopers
- Re: Array Type Mismatch
- From: Michel Walsh
- Re: Array Type Mismatch
- From: Michel Walsh
- Re: Array Type Mismatch
- Prev by Date: Re: Form vs. recordset
- Next by Date: RE: Array Type Mismatch
- Previous by thread: Re: Array Type Mismatch
- Next by thread: Re: Array Type Mismatch
- Index(es):
Relevant Pages
|