1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

code for keeping entered data constant

Discussion in 'Visual Basic ( VB )' started by viv345, Mar 7, 2010.

  1. viv345

    viv345 New Member

    Joined:
    Feb 22, 2010
    Messages:
    76
    Likes Received:
    0
    Trophy Points:
    0
    How can i keep my data constant after quitting from program. For example:-
    If i have entered Potato in any text box. when quit and reopen the program the word potato remains their.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Save them to a file and reload the content from file when you run next time.
     
  3. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    or you can use the registry to accomplish that.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    I don't think that is a good idea and any application specific data should be stored in registry and not user specific data of what he enters. Just semantics.
     
  5. viv345

    viv345 New Member

    Joined:
    Feb 22, 2010
    Messages:
    76
    Likes Received:
    0
    Trophy Points:
    0
    How? Any example. Please
    Thanks
     
  6. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    first of all name your vb version
    for example vb6 , vb2008
     
  7. viv345

    viv345 New Member

    Joined:
    Feb 22, 2010
    Messages:
    76
    Likes Received:
    0
    Trophy Points:
    0
    In VB6
     
  8. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    with the file method
    ==================
    you need a form with a textbox named text1

    Code:
    [COLOR=Blue]Option Explicit[/COLOR]
    Dim filename As String
    Dim filepath As String
    Dim text As String
    
    [COLOR=Blue]Private Sub Form_Load()[COLOR=Red]'[/COLOR][/COLOR][COLOR=Red]when you load form ,checks if the file exists and reads data[/COLOR] and stores to text var. and textbox1
    filename = "datastore.txt"
    filepath = App.Path + "\" + filename
    Dim f As Integer
    f = FreeFile
    If Dir(filepath) = filename Then
    Open filepath For Input As #f
    Line Input #f, text
    Text1.text = text
    Close #f
    End If
    [COLOR=Blue]End Sub[/COLOR]
    
    [COLOR=Blue]Private Sub Form_Terminate()[/COLOR][COLOR=Red]'when the form terminates,exits-->save text to file[/COLOR]
    Dim f As Integer
    f = FreeFile
    Open filepath For Output As #f
    Print #f, text
    Close #f
    [COLOR=Blue]End Sub[/COLOR]
    
    [COLOR=Blue]Private Sub Text1_Change()[/COLOR][COLOR=Red]'when you alter text save it to text variable[/COLOR]
    text = Text1.text
    [COLOR=Blue]End Sub[/COLOR]
    
    
    
     
  9. viv345

    viv345 New Member

    Joined:
    Feb 22, 2010
    Messages:
    76
    Likes Received:
    0
    Trophy Points:
    0
    Tried:
    Given : filepath = App.Path + "\" + filename as C:\Datastore.text
    and form 1 with text1.text
    BUT IT not worked
    Project attached

     

    Attached Files:

  10. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    the program works great when you press X to close your form.

    I never told you anything about a command button.
    If you want to end your program with a command button then before "end"
    copy paste the form_terminate() code

    like the code below

    Code:
    Private Sub [COLOR=Blue]Command1_Click()[/COLOR]
    [COLOR=Red]Dim f As Integer
    f = FreeFile
    Open filepath For Output As #f
    Print #f, text
    Close #f[/COLOR]
    [COLOR=Blue]End
    End Sub[/COLOR]
    
    
     
  11. viv345

    viv345 New Member

    Joined:
    Feb 22, 2010
    Messages:
    76
    Likes Received:
    0
    Trophy Points:
    0
    Thanks it is working
    But if i like to delete or clear the text from text1.text, will the procedure will be same as command1_Click() is given?
     
  12. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
  13. viv345

    viv345 New Member

    Joined:
    Feb 22, 2010
    Messages:
    76
    Likes Received:
    0
    Trophy Points:
    0
    Thanks
     

Share This Page