Go Premium for a chance to win a PS4. Enter to Win


saving settings in visual basic

Posted on 2003-11-13
Medium Priority
Last Modified: 2010-04-17
ello all!

again, its that damn IT coursework that just wont sort itself out! im sooo close, but i underestimated one lil thing...

basically ive got a login system. usernames are entered into a listbox (cunningly named lstUser) by an administrator. students can then log on to the program and take tests, the results of which are sent to the tutors email address so they can check the students progress.

i need to learn how to save settings so that the names in the listbox are remembered each time the program is opened. pretty pointless if the are lost each time the administrator closes the program!

in the same way, access to the admin section of the program is done via a password. this password can be changed (don't know why they would want to change it, but i thought it made a cool USP)

i need a way to save the settings so that this password is remembered, in much the same way as the data in the list box is entered. if it makes any difference, the password is stored in a label on the admin section, the names being in a listbox (as mentioned).

if its possible, would some kind person please give me a crash course in saving settings? code is great, but it would also help if it was fully explained (how and why it works) - im not too quick im afraid (damn welsh heritage) =)

thankee kindly all


ps. sorry its only 115 points - its all ive got =(
Question by:d-richardson
  • 3

Accepted Solution

wsteegmans earned 345 total points
ID: 9743933
Where is your VB-Program located? On a shared folder ... so the students can start it form this shared folder? Or is every program (EXE) installed on each computer?

If I understand your question, you want that all the usernames entered by the Administrator are saved ... So, when a student starts the program, he can pick his username and log in ... Am I right?

For this purpose, you can use a database (for example Access or SQL-Server) where you store all your info (usernames, passwords, ...) or you can create a textfile where you save all your data. But I prefer the database.

Tutorial about VB and DB-programming (Part I through IV)

If you want that everytime the program is started, the last entered studentname is shown (so your program remembers the last user), you can store it in the Windows Registry.
A nice tutorial about the Windows Registry and using it in VB:

Author Comment

ID: 9743984
the program will eventually be in a shared folder.

If I understand your question, you want that all the usernames entered by the Administrator are saved ... So, when a student starts the program, he can pick his username and log in ... Am I right?

-spot on!

the article about the windows registry looks to be what im looking for. il have a play around, see what happens:) thank you for your help



Expert Comment

ID: 9744007
There are about 1,000 ways to do this - and you may want to consider using a database or XML if you want to maintain data about the different students.  The easiest way to solve this problem is to store the user names and passwords in a text file.
For example, you've got a file called "users.txt" in your application directory with the following lines:


You could populate the listbox like so:

Private Sub Form_Load()

Dim UserFile As Integer ' Reference to the file
Dim strUserName As String 'the user name from the file
UserFile = FreeFile       ' Get the first available file
'VB uses numbers to keep track of files

Open App.Path & "\" & "users.txt" For Input As UserFile 'Open the file "users.txt" thats in your program directory

Do While Not EOF(UserFile)
Line Input #UserFile, strUserName
'store the line from the file in strUserName
lstUsers.AddItem (strUserName) ' add the user name to the list

Close UserFile ' close the file so it can be accessed

End Sub


Author Comment

ID: 9747317
played around with what u suggested. the annotations were great! i actually understand what u mean!

however, the problem would be that if i have a file called "adminPswrd.txt" or something, some bright spark is probably going to think of having a look at it.. then changing it.. much to the annoyance of the tutor. similiarly, if i have a .txt file which contains the students usernames, someone is going to decide to have a laugh.

 being a student myself, i spend a lot of time trying to make the lives of the administrators of my college's IT department difficult! as a result, i know just what sort of treatment my program will recieve and have written it around them. for example, ive taken mesures to ensure that students cant use excel macros to guess the admin password etc.

i think using the windows registry will be my best bet. a friend gave me some code last night that does work:

Private Sub btnSave_Click()

Dim CatStr As String, i As Integer
  Dim allUser, dUser As Integer
  allUser = GetAllSettings("administration", "usernames")
  If IsArray(allUser) Then
    For dUser = LBound(allUser, 1) To UBound(allUser, 1)
      DeleteSetting "administration", "usernames", allUser(dUser, 0)
  End If
  CatStr = ""
  If AdminForm.lstUser.ListCount > 0 Then
    For i = 0 To AdminForm.lstUser.ListCount
      If Trim(AdminForm.lstUser.List(i)) <> "" Then
        SaveSetting "administration", "usernames", "Item_" & i, Trim(AdminForm.lstUser.List(i))
      End If
  End If

End Sub

Private Sub Form_Load()

  Dim allUser, dUser As Integer
   allUser = GetAllSettings("administration", "usernames")
  If IsArray(allUser) Then
    For dPswrd = LBound(allUser, 1) To UBound(allUser, 1)
      lstUser.AddItem allUser(dUser, 1)
  End If

the problem being that i dont properly understand it! i keep playing around, but i cant make head or tails of it. the tutorial suggested in the first response to my question is helping, but still having trouble. any chance i can be a cheeky sod and ask an expert to explain? havnt got any points left to give im afraid, so dont want to waste anyones time




Author Comment

ID: 9747688

i figured it all out! its all cool:)

just gotta hope i can access the windows registry on the computers at college... :|

ah well, thank you for all your help everyone. this place is amazing! must confess i dont understand anywhere near half of it, but i intend to persevere

cheers for all the help you've given to a naive programmer such as myself - much appreciated!

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Starting up a Project

824 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