View Single Post
Old 06-17-2009   #3 (permalink)
T Lavedas


 
 

Re: Trouble with finding a space useing Instr

On Jun 17, 10:56*am, OldDog <mikef2...@xxxxxx> wrote:
Quote:

> Hi,
>
> I am trying to use Instr to find the model number of seveal different
> types of computers. I am getting and error at strC.
>
> Here is the code:
>
> 'Determine the model number of the PC
> Set CompSys = objWMIService.ExecQuery("Select * from
> Win32_ComputerSystem",,48)
> For Each sysItem In CompSys
> * * * * fmfg = Trim(sysItem.Manufacturer)
> * * * * fmm = Trim(sysItem.Model)
> * * * * Wscript.Echo fmm
>
> arrComputers = Array("D420","D430","D620","D630", _
> * * * * * * * * "E4300","E6400","745","755", _
> * * * * * * * * "760","490","690", _
> * * * * * * * * "M4300","M4400","M65","T3400", _
> * * * * * * * * "T5400","T7400","dc7600","dc7700", _
> * * * * * * * * "dc7800","T60","T61")
> For Each strA In arrComputers
> * * *strB = Instr(1,fmm,strA)
> * * *strC = Instr(strB,fmm,(CHr(32))) <---- Error is here
> * * *strD = Mid(fmm,strB,strC)
>
> Wscript.Echo strD
>
> And here is the error:
>
> Microsoft VBScript runtime error: Invalid procedure call or argument:
> 'Instr'
>
> Any help would be appriciated.
First strA and strB are not strings, but rather they are numbers - so
their names are misleading. That may also be a part of your problem,
in that I think your logic is faulty.

Specifically, the result of the first search returns the value zero,
if the search fails. Since strings are not zero based, the second
search fails with the error message you cite. Therefore, you need to
test the result and skip the rest if it is zero, that is if the
computer under test does not match the content of strA.

I must presume the contents of the loop variable, strA, does not
represent the entire string you are searching for. Otherwise, the
rest of the parsing makes no sense. However, the next problem will
arise, in there is no trailing space after the starting point you have
located. The solution, I generally use is to just past a space at the
end of the string under test to make sure that doesn't happen, maybe
something like this ...

For Each strA In arrComputers
numB = Instr(1, fmm, strA)
if numB <> 0 then
numC = Instr(numB, fmm & " ", Chr(32)) - 1
strD = Mid(fmm,numB,numC)

Wscript.Echo strD
end if
'...
next ' strA

Tom Lavedas
***********
My System SpecsSystem Spec