Solved

Open & Save 2 Dimensional Array

Posted on 2002-05-15
7
173 Views
Last Modified: 2010-05-02


Hello All Experts :

OK – All I need help with is saving the data I have in a Two-Dimensional Array, and then of course bringing
it back from the file. I have made the forms, buttons etc, etc, and I think (but have not fully tested)
my array works fine.

The bottom line is, when the user gets to the last page (Page 2) of the survey, they hit next response
to go to a new one. Or, previous should they need to go back. The maximum number is 500.

When they are done, they need to hit finish, and a message box must confirm that they want to save the
result. When this happens, a common dialog should appear asking for a file name. (Text File Format,
Comma Delimited)

For Open – Common Dialogue should appear asking the user to select a file. The data is loaded into the
array, and the user should then see the results starting at Page One, Result One. Should the user make
ANY changes, this must be noted, and when the program exits, it MUST be confirmed that changes have
been made and if the users wants to update the file.

I have made a basic outline of the file in the results directory.
So you can work with my project, I have uploaded it to the net for anyone to download. If your not happy
with the way I have coded something (considering I’m a newbie) please feel free to change it.

http://www.hypermax.net.au/~user/hollstar/vbec3.zip

This program is for an assignment I’m working on and is not for public use.
0
Comment
Question by:hollstar
  • 4
  • 3
7 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 7010844
You deleted the other question just as I posted some code for you anyway:

Add this to frmMain

Private Sub OpenFile()
    On Error Resume Next
    With CommonDialog1
        .CancelError = True
        .Filter = "Comma Delimited Values (*.txt,*.csv)|*.csv;*.txt|All Files (*.*)|*.*"
        .ShowOpen
        If Err.Number = 0 Then 'Not cancelled
            LoadFromFile .FileName
            tlbMain_ButtonClick tlbMain.Buttons("New")
        End If
    End With
End Sub

Private Sub SaveFile()
    On Error Resume Next
    With CommonDialog1
        .CancelError = True
        .Filter = "Comma Delimited Values (*.txt,*.csv)|*.csv;*.txt|All Files (*.*)|*.*"
        .ShowOpen
        If Err.Number = 0 Then 'Not cancelled
            SaveToFile .FileName
        End If
    End With
End Sub

And call these subs from the appropriate part of the toolbar buttonclick event.

Drop these subs/functions into your module to support this:

Public Sub LoadFromFile(ByVal FileName As String)
    Dim intFHandle As Integer
    Dim intElement As Integer
    Dim strLineContent As String
    intElement = 1
    intFHandle = FreeFile()
    Open FileName For Input As #intFHandle
    Do
        Line Input #intFHandle, strLineContent
        PopulateElement intElement, strLineContent
        intElement = intElement + 1
    Loop Until EOF(intFHandle)
    Close #intFHandle
End Sub

Private Sub PopulateElement(ByVal Element As Integer, ByVal Content As String)
    Dim aryElements() As String
    Dim intElement As Integer
    aryElements = Split(Content, ",")
    For intElement = 0 To 7
        mvarQuestions(Element, intElement) = aryElements(intElement)
    Next
End Sub

Public Sub SaveToFile(ByVal FileName As String)
    Dim intFHandle As Integer
    Dim intElement As Integer
    intFHandle = FreeFile()
    If Dir(FileName) <> "" Then
        Kill FileName
    End If
    Open FileName For Output As #intFHandle
    For intElement = 1 To 500
        Print #intFHandle, GetRow(intElement)
    Next
    Close #intFHandle
End Sub

Private Function GetRow(ByVal Element As Integer) As String
    Dim strRow As String
    For intElement = 0 To 7
        strRow = strRow & mvarQuestions(Element, intElement) & IIf(intElement = 7, "", ",")
    Next
End Function

You will notice that though this information can be saved and loaded you force the date to be the current date regardless of the value stored in the file, have a look at your change event for this and perhaps include a check which only sets this as a default if the relevant array element is empty.

As for managing changes and forcing a save, you should set a global variable (boolean) and when a change is made set this to True, then when you exit the program you can test this boolean value and ask whether a save is required, if so the use the save code I have given you.
0
 

Author Comment

by:hollstar
ID: 7010887
Did you test this with my own code ? I assume you did and hence I guess it worked for you... Didnt for me... Save box came up, but not data...


Also, when it saves, it should be a text file, buttom just comma delimited...


I will also look into this more... Thanks for your help...
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7010925
hollstar, didn't test it enough obviously. Here is the necessary mod:

Private Function GetRow(ByVal Element As Integer) As String
    Dim strRow As String
    Dim intElement As Integer
    For intElement = 0 To 7
        strRow = strRow & mvarQuestions(Element, intElement) & IIf(intElement = 7, "", ",")
    Next
    GetRow = strRow
End Function

Also I think you need to change the SaveFile sub to have .ShowSave instead of .ShowOpen - my fault I copied from the openfile bit and forgot to change the dialog type.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:hollstar
ID: 7012297
Yeppa - Checked That Out...


Only thing is, with my loading code for the information itself, it doesnt display the info for the first page, and it will also get an error for the new page after the last one in the file...


Could you offer any error trapping for this ?
0
 

Author Comment

by:hollstar
ID: 7019353
OK - I fixed a few of the problems but I still have not had a good chance to look into the following hence any ideas would be good...

When you load up a file, it does not display the data... You have to go next and then back to see it...

And also, you get an error when you reach the end of the file.


One other option I would like to include in this is to allow the user to go the first,last & search for a result... Could you help with this ? If so please do so... Should you want a further 50 points or so, this is not a problem.
0
 

Author Comment

by:hollstar
ID: 7023930
TimCottee :

While your code worked in the main ways I required, I felt that (assuming you have been available to make further posts) you have been unwilling to make further contributions towards my question due to the few extra questions I asked in relation to this question as well as fixing other small bugs.

Because I am no expert, by any stretch of the imagination, it has taken me until now, (on and off) to fix any problems I have had to a (I hope) satisfactory level. While I am awarding you the points, I am only offering a “B” result for the above fact. Please understand I’m sure you are very busy, as am I, however I do feel that if you think you can not follow up on a question you answer, then don’t offer one.

Anyway, hope to see you around EE more often… I know I will need it ! ;)

Hollstar
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7023998
My apologies Hollstar, sometimes I don't have as much time as I would like to keep up to date on my active threads, I currently have approx 1000 threads in 4 different forums open so occasionally things like this slip through the net. I am not one of these experts that absolutely have to have an 'A' grade though I appreciate them. I am glad that you managed to iron out the quirks for yourself and would be happy to help in the future should I see another question from you.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now