[Last Call] Learn how to a build a cloud-first strategyRegister Now


simple question

Posted on 2004-11-26
Medium Priority
Last Modified: 2010-05-02
i am new to vb:

this is what i want to do

i have a combobox with a list of items:
each of these represent domain names


i also have a command button called  'execute'

now when user selects on either of this , say a1, and hits 'execute' my application needs to go to


and add the entry to the hosts file

such as

vv.xx.yy.zz          a1.go.domain.com
vv1.xx1.yy1.zz1   a1.see.domain.com

similarly if user clicks on a2 and hits execute

vv.xx.yy.zz          a1.go.domain.com
vv1.xx1.yy1.zz1   a1.see.domain.com

i now the ip addresses vv.xx.yy.zz etc, so i need to know how to do file handling in vb.

this is what is intend to do...

when user selects one of a1,a2 etc,

backuo hosts to host.bak
delete hosts
create a new file 'hosts' using vb
programmatically write  

vv.xx.yy.zz          a1.go.domain.com
vv1.xx1.yy1.zz1   a1.see.domain.com

to the hosts file

close the file.

please guide me how i can achieve this VB programmatically. i am not familiarwith VB programming constructs.

Question by:Vlearns
  • 4
  • 3
LVL 18

Accepted Solution

JR2003 earned 2000 total points
ID: 12684196
Include a reference to the "Microsoft Scripting Runtime" in your Project\References

Private Sub Command1_Click()
    Dim fso As Scripting.FileSystemObject
    Dim MyFile As Scripting.TextStream
    With fso
        'Back up the file
        .CopyFile "C:\WINDOWS\system32\drivers\etc\hosts.txt", "C:\WINDOWS\system32\drivers\etc\hosts.bak", True
        Set MyFile = .OpenTextFile("C:\WINDOWS\system32\drivers\etc\hosts.txt", ForAppending, True)
    End With
    With MyFile
        .WriteLine "vv.xx.yy.zz          a1.go.domain.com"
        .WriteLine "vv1.xx1.yy1.zz1 a1.see.domain.com"
    End With
End Sub
LVL 18

Expert Comment

ID: 12684199
To access the values in the combobox:

Dim sSelectedItem as String
sSelectedItem  = Combo1.List(Combo1.ListIndex)

Author Comment

ID: 12684232
the hosts file should be just 'hosts' and not hosts.txt
do you think the opentextfile can only create the hosts file with hosts.txt?
i will try and see

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Comment

ID: 12684258
got this error

Object Variable or With Block Variable not set


.CopyFile "C:\WINDOWS\system32\drivers\etc\hosts", "C:\WINDOWS\system32\drivers\etc\hosts.bak", True

shouldnt fso be initialized to somethin?

Author Comment

ID: 12684267

solved the problem:

should have been

Dim fso As New Scripting.FileSystemObject
instead ogf

Dim fso As Scripting.FileSystemObject


Author Comment

ID: 12684363

can someone tell me how to write this to file

.WriteLine "  & Item & .domain.com "

where the output should be  ab.domain.com

where Item="ab"  ????????????????????????

LVL 18

Expert Comment

ID: 12685166
<<<<can someone tell me how to write this to file  .WriteLine "  & Item & .domain.com "

.WriteLine " "  &  Combo1.List(Combo1.ListIndex) & ".domain.com"

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month17 days, 14 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question