Type Mismatch error

V

Varun

Guys,

Can someone please tell me why I get type mismatch error. The problem line
is:

layer_array = geomsasciiparse()

This line in the Sub below calls out the function named geomsasciiparse
which returns an array.

Any help is appreciated.


Private Sub CommandButton1_Click()
Dim layer_array() As Variant



path1 = MentDesContPath

layer_array = geomsasciiparse()

MsgBox "Done"

End Sub

Public Function geomsasciiparse() As Variant

Dim logical_layer() As Variant
Dim logicallayernumber, arraynumber As Integer

Dim objFSO As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objGeomsAsciiFile As Object

'Buf array stores words from each line
Dim Buf() As String

'opening geoms_ascii file as read only from Mentor Design Container
Set objGeomsAsciiFile = objFSO.OpenTextFile(path1 & "\geoms_ascii", 1)

Do While Not objGeomsAsciiFile.AtEndOfStream
strLine = objGeomsAsciiFile.Readline

'look for first instance of _LAYER_DEFNITION in geoms_ascii
If InStr(strLine, "_LAYER_DEFINITION") <> 0 Then

Buf() = qc_Split(strLine)

'extract logical layer number from ARTWORK_01_LAYER_DEFNITION
logicallayernumber = Mid(Buf(1), 9, 2)

'need to redim logical_layer so it can be assigned to geomsasciiparse later
ReDim Preserve logical_layer(logicallayernumber)

'ignore when ARTWORK_LAYER_DEFINITION = 00 in geoms_ascii
If logicallayernumber <> "00" Then

For buf_idx = 1 To UBound(Buf)

If InStr(Buf(buf_idx), "SIGNAL") <> 0 Or _
InStr(Buf(buf_idx), "POWER") <> 0 Then

logical_layer(logicallayernumber) = Buf(buf_idx)
Exit For
End If
Next
End If
End If
Loop

geomasasciiparse = logical_layer

objGeomsAsciiFile.Close

End Function
 
R

ryguy7272

Just hazarding a guess here. Shouldn't the
Dim layer_array() As Variant
be just
Dim layer_array As Variant
Why do you have the () in there?
I don't believe I've seen it done that way before.

HTH,
Ryan---
 
V

Varun

Thanks Ryan but didn't help.

I tried your suggestion and though I do not get the type mismatch error, the
value of layer_array is empty.

layer_array without () wouldn't make layer_array an array or would it (just
asking)?

What I am after is taking the value of geomasciiparse (which is an array
since logical_layer is an array in the Function geomsasciiparse) and assign
it to another array in the Sub.

Any other pointers? Thanks for help.
 
M

MrRadish

Ryan is correct about your declaration. The declaration:

dim x() as variant

Actually declares an array of variants. This will be a type mismatch for a
function that returns an array. You do not want the parentheses.

There are other errors. In your function you make the same mistake with
declaring the variant. There are other type issues as well. I have gone
through and commented them below (and corrected where possible). I may have
missed some.

You want to use option explicit at the beginning of your code modules
(forces you to declare variables). You also want to explicitly declare
variables where possible so that you don't fall into any type errors.



Private Sub CommandButton1_Click()
Dim layer_array As Variant

path1 = MentDesContPath
layer_array = geomsasciiparse()
MsgBox "Done"

End Sub

Public Function geomsasciiparse() As Variant
Dim logical_layer As Variant ' Again, you don't want to use parentheses -
you have a single variant (which is an array), not an array of variants
Dim logicallayernumber, arraynumber As Integer ' You are just declaring
arraynumber as an integer, logicallayernumber is a variant

Dim objFSO As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim objGeomsAsciiFile As Object

'Buf array stores words from each line
Dim Buf() As String

'opening geoms_ascii file as read only from Mentor Design Container
Set objGeomsAsciiFile = objFSO.OpenTextFile(path1 & "\geoms_ascii", 1)

Do While Not objGeomsAsciiFile.AtEndOfStream
strLine = objGeomsAsciiFile.Readline

'look for first instance of _LAYER_DEFNITION in geoms_ascii
If InStr(strLine, "_LAYER_DEFINITION") > 0 Then
Buf() = qc_Split(strLine)

'extract logical layer number from ARTWORK_01_LAYER_DEFNITION
logicallayernumber = Mid(Buf(1), 9, 2) ' This is now a string due to use
of mid function

'need to redim logical_layer so it can be assigned to geomsasciiparse later
ReDim Preserve logical_layer(CInt(logicallayernumber)) ' You can't redim
using a string as parameter

'ignore when ARTWORK_LAYER_DEFINITION = 00 in geoms_ascii
If logicallayernumber <> "00" Then
For buf_idx = 1 To UBound(Buf)
If InStr(Buf(buf_idx), "SIGNAL") <> 0 Or _
InStr(Buf(buf_idx), "POWER") <> 0 Then
logical_layer(CInt(logicallayernumber)) = Buf(buf_idx)
Exit For
End If
Next
End If
End If
Loop
geomasasciiparse = logical_layer
objGeomsAsciiFile.Close

End Function
 
M

MrRadish

Actually, there were more errors. In the function, you need to declare your
array as that, not as a variant. Also, you cannot redim preserve without
having first dimmed.

Public Function geomsasciiparse() As Variant
Dim logical_layer() As string ' This is not a variant, it is an array of
strings.
Dim logicallayernumber, arraynumber As Integer ' You are just declaring
arraynumber as an integer, logicallayernumber is a variant

Dim objFSO As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim objGeomsAsciiFile As Object

'Buf array stores words from each line
Dim Buf() As String
Redim logical_layer(0) ' You cannot redim preserve without first dimming
the array (the preserve comes later)

'opening geoms_ascii file as read only from Mentor Design Container
Set objGeomsAsciiFile = objFSO.OpenTextFile(path1 & "\geoms_ascii", 1)

Do While Not objGeomsAsciiFile.AtEndOfStream
strLine = objGeomsAsciiFile.Readline

'look for first instance of _LAYER_DEFNITION in geoms_ascii
If InStr(strLine, "_LAYER_DEFINITION") > 0 Then
Buf() = qc_Split(strLine)

'extract logical layer number from ARTWORK_01_LAYER_DEFNITION
logicallayernumber = Mid(Buf(1), 9, 2) ' This is now a string due to use
of mid function

'need to redim logical_layer so it can be assigned to geomsasciiparse later
ReDim Preserve logical_layer(CInt(logicallayernumber)) ' You can't redim
using a string as parameter

'ignore when ARTWORK_LAYER_DEFINITION = 00 in geoms_ascii
If logicallayernumber <> "00" Then
For buf_idx = 1 To UBound(Buf)
If InStr(Buf(buf_idx), "SIGNAL") <> 0 Or _
InStr(Buf(buf_idx), "POWER") <> 0 Then
logical_layer(CInt(logicallayernumber)) = Buf(buf_idx)
Exit For
End If
Next
End If
End If
Loop
geomasasciiparse = logical_layer
objGeomsAsciiFile.Close

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top