Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista Tutorial - Trouble with finding a space useing Instr

Reply
 
Old 06-17-2009   #1 (permalink)
OldDog
Guest


 
 

Trouble with finding a space useing Instr

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.



My System SpecsSystem Spec
Old 06-17-2009   #2 (permalink)
ekkehard.horner
Guest


 
 

Re: Trouble with finding a space useing Instr

OldDog schrieb:
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
As Instr() returns a (numerical) Position, all your
variable names are guilty of type prefix fraud.
Quote:

> strB = Instr(1,fmm,strA)
strB (horror) will hold 0 (zero), if strA isn't found
Quote:

> strC = Instr(strB,fmm,(CHr(32))) <---- Error is here
you can't call Instr() with a (numerical) starting position
of 0.
----
Quote:
Quote:

>> nPos = Instr( 0, "x", "y" )
>>
Error Number: 5
Error Description: Ungültiger Prozeduraufruf oder ungültiges Argument
(invalid procedure call or invalid argument)
Quote:
Quote:

>> quit
----
You need something like:

For Each strA In arrComputers
nStart = Instr( 1, fmm, strA )
If 0 < nStart Then
nEnd = Instr( nStart, fmm, " " )
If 0 < nEnd Then
' cut
Else
' surprise
End If
Else
' try next
End If
Next


Quote:

> 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.
>
>
My System SpecsSystem Spec
Old 06-17-2009   #3 (permalink)
T Lavedas
Guest


 
 

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
Old 06-17-2009   #4 (permalink)
OldDog
Guest


 
 

Re: Trouble with finding a space useing Instr

On Jun 17, 10:42*am, T Lavedas <tglba...@xxxxxx> wrote:
Quote:

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

> > Hi,
>
Quote:

> > I am trying to use Instr to find the model number of seveal different
> > types of computers. I am getting and error at strC.
>
Quote:

> > Here is the code:
>
Quote:

> > '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
>
Quote:

> > 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)
>
Quote:

> > Wscript.Echo strD
>
Quote:

> > And here is the error:
>
Quote:

> > Microsoft VBScript runtime error: Invalid procedure call or argument:
> > 'Instr'
>
Quote:

> > 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
> ***********- Hide quoted text -
>
> - Show quoted text -
Thanks guys, my faulty logic was on full display there. It's working
well.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
BSoD - trouble finding solution... Vista General
trouble finding sata driver Drivers
Trouble Finding Text in Windows Mail Vista mail
Finding Used Memory Address Space; Maximum amount of RAM Vista hardware & devices
Finding Used Memory Address Space; Maximum amount of RAM Vista performance & maintenance


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46