Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

Recommended Posts

I have a VBS that I run to create a new folder for a new part. I have it set up to ask for customers name then part number. Is there any way to have to create a table or something with all the big customers. then when the VB askes me for cutomer I could just put in 1 or 2 or 3 ....? Have the last option be other. If customer name = other then I would type in new name. Does this make any since????

Link to comment
Share on other sites

Hello,

 

I believe there is a way (and not only one).

 

Here is how I would do it:

- create a txt file (like customers.txt). this file will contain your customers (every line - one customer).

- use functions like

code:

  OpenAsTextStream 

to read this file.

 

Here is some small example: of reading a text file line by line:

code:

strfile = c:customer.txt  'path to your txt file

Set FSO2 = CreateObject("Scripting.FileSystemObject")

Set TS = FSO2.GetFile(strFile)

Set fsoTextFile = TS.OpenAsTextStream(1, -2)

While fsoTextFile.AtEndOfLine <> True

customer = fsoTextFile.ReadLine

Wend

- there is of course some more things to do:

every time the while loop reads a line the script should count the number of the line (like: customer # = line #), if the input number of the customer is some other text (for instance: new customer name) the script shoud not go into the read file loop. Instead it should write a new line into the file at the and of this file (look at the help file for the writing mathod).

- of course you can use similar methods to read the whole file and determine the number of customers, their names,... This information can help you improve user interface.

 

I believe there are some more experineced VBS gurus on this forum like BULLINESS, etc., who can help much more than I can (I believe they would do it some other, more efficient way).

 

I hope you will find this information useful.

 

Pkrzic

Link to comment
Share on other sites
  • 5 weeks later...

Below is an example of reading a text file that has a list of customer names and assigning it to an array. The user is prompted for a number that represents a customer index in the list, and then the customer name is retrieved from the array via the index and displayed to the user.

 

There is no error checking at all so you would want to add that in a real world script.

 

 

code:

 

'////////////////////////////////////////////////////////////////////////////////

'//

'// Author: [email protected] Mick George

'// Date: 10/04/2006 09:07 AM

'// File Name: connormac.vbs

'//

'// Description: Mastercam VB Script

'//

'// Comments: Example of reading a flat file

'//

'////////////////////////////////////////////////////////////////////////////////

 

 

'///////////////// My Constants /////////////////

 

' -- Edit path to suit

Const CUSTOMER_INFO = "C:Customer.txt"

 

' Text file has a simple list of customers

'Acme Industries

'CNC Software

'Joe Blow Inc.

 

 

' -- Start Script

Call Main

 

 

' ////////////////////

' Sub Declaration

' ////////////////////

Sub Main()

 

Dim CustomerList

Dim response

Dim selectedCustomer

 

' -- Build the list

CustomerList = GetCustomerList()

 

' -- Prompt for a #

If askNumber("Select a customer #", 1, 100, response) Then

 

' -- Get the customer

selectedCustomer = GetCustomerName(response, CustomerList)

 

If selectedCustomer = "" Then

' -- You could add this customer to your textfile

ShowString "New Customer!"

Else

' -- We have our customer name

ShowString selectedCustomer

End If

 

End If

 

End Sub

 

' -- Pass in the array of customers and the index and return

' -- the customers name at that index

Function GetCustomerName(idx, theList)

 

Dim i

 

For i = 1 To Ubound(theList)

If i = idx Then

GetCustomerName = theList(i)

Exit Function

End If

Next

 

' -- Not found so return null

GetCustomerName = ""

 

End Function

 

 

' -- Open text file and fill an array

' -- with the list of our customers

Function GetCustomerList()

 

Dim FSO, fsoStream

Dim CustomerList()

Dim i

Dim customer

 

Set FSO = CreateObject("Scripting.FileSystemObject")

 

Set fsoStream = FSO.OpenTextFile(CUSTOMER_INFO)

 

' -- Start at 1

i = 1

 

Do While fsoStream.AtEndOfStream <> True

 

customer = fsoStream.ReadLine

 

If Len(customer) > 0 Then

Redim Preserve CustomerList(i)

CustomerList(i) = customer

i = i + 1

End If

 

Loop

 

fsoStream.Close

 

' -- Return

GetCustomerList = CustomerList

 

 

End Function

 

 


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...