By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,414 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,414 IT Pros & Developers. It's quick & easy.

New, Open, Save, Save As, Close

P: n/a
Hi VB fans

I'm working on a relatively large project in VB6 with about a dozen
forms, including graphs, labels, text boxes, etc. The software itself
is actually a flow simulator with more or less complex technical
calculations, several input variables. I would like to equipp the
starting panel with the usual New, Open, Save, Save As, Close etc.
menus (like in Excel, or Word, etc.) What is the best way to
accomplish Save, or Save As? Should be the variables behind the entry
fields be written to a binary file, or can the forms be saved directly
with all the controls and it contents to a file, or is there any other
solution? How professionals do it?

Any advice is highly appreciated. Tx in advance

Andras
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Andras Gilicz" <ag*****@chello.hu> wrote in message
news:72**************************@posting.google.c om...
| Hi VB fans
|
| I'm working on a relatively large project in VB6 with about a dozen
| forms, including graphs, labels, text boxes, etc. The software itself
| is actually a flow simulator with more or less complex technical
| calculations, several input variables. I would like to equipp the
| starting panel with the usual New, Open, Save, Save As, Close etc.
| menus (like in Excel, or Word, etc.) What is the best way to
| accomplish Save, or Save As? Should be the variables behind the entry
| fields be written to a binary file, or can the forms be saved directly
| with all the controls and it contents to a file, or is there any other
| solution? How professionals do it?
|

I would certainly save the values of the entry variables, not make an
attempt to save the forms themselves.

You can choose an ordinary file, or even use an Access database if it
suits you.

For a file, I would write all variables in plain text, not in actual
binary. It makes debugging, etc. much easier. Using Input# and Write# to
read and write the file will produce this result, and take care of the
formatting. (Write# does not handle strings with embedded quotes,
however).


Jul 17 '05 #2

P: n/a
Andras Gilicz wrote:
Hi VB fans

I'm working on a relatively large project in VB6 with about a dozen
forms, including graphs, labels, text boxes, etc. The software itself
is actually a flow simulator with more or less complex technical
calculations, several input variables. I would like to equipp the
starting panel with the usual New, Open, Save, Save As, Close etc.
menus (like in Excel, or Word, etc.) What is the best way to
accomplish Save, or Save As? Should be the variables behind the entry
fields be written to a binary file, or can the forms be saved directly
with all the controls and it contents to a file, or is there any other
solution? How professionals do it?

Any advice is highly appreciated. Tx in advance

Andras

G'day Andras,
search the Help file for "Using Menus in Your Application" this topic is
well documened there.
and/or
Go to >Tools >Menu Editor
cheers,
build
Jul 17 '05 #3

P: n/a
build <bu*****@datafast.net.au> wrote in message news:<41******@news.alphalink.com.au>...
Andras Gilicz wrote:
Hi VB fans

I'm working on a relatively large project in VB6 with about a dozen
forms, including graphs, labels, text boxes, etc. The software itself
is actually a flow simulator with more or less complex technical
calculations, several input variables. I would like to equipp the
starting panel with the usual New, Open, Save, Save As, Close etc.
menus (like in Excel, or Word, etc.) What is the best way to
accomplish Save, or Save As? Should be the variables behind the entry
fields be written to a binary file, or can the forms be saved directly
with all the controls and it contents to a file, or is there any other
solution? How professionals do it?

Any advice is highly appreciated. Tx in advance

Andras

G'day Andras,
search the Help file for "Using Menus in Your Application" this topic is
well documened there.
and/or
Go to >Tools >Menu Editor
cheers,
build


Ok, tx build, I have no problems with menus, I've got a lot of them,
but the question was which code to put behind the Save or Save As
menus, i.e. how professionally the entry field variables on several
forms can be saved up and reloaded if needed later. How Excel does it
for e.g.? Steve's solution above is sympathetic, tx Steve!

regards

Andras
Jul 17 '05 #4

P: n/a
Andras,

One technique that I've used in the past is to use an Access database (coded
with ADO in VB), and then put the name of each field in it's corresponding
input control's Tag property. You can then use a For Each Control type of
loop to easily get the control's value and store it to the field named in
it's tag property. The same process works in reverse to load a previously
saved group of input values.

To take this a step further, if you have combo boxes with a list of values
to select from, you can write a generic routine to load the lists and then
use a text file to store all the values for each listbox. This makes adding
or removing an item from a listbox a simple matter of editing the text file.
Again, use the tag property to correlate the control with it's list of
values in the file.

Here are some routines I wrote a few years ago to do this. These are copied
straight from my projects, so you'll need to tweak them, but you'll get the
idea. (This project stored engineering design data, so records are loaded
and stored based on a "Design ID" field. It's just an Access AutoNumber
field.

(InputDatabase is a global variable that points to the Access database)

*****************

Public Sub LoadDesignFromDB(Activeform As Form, DesignID As Long)

' This routine loads the contents of all controls on the main form that
contain a tag value
' from the selected record in the design database

Dim CurrentControl As Control
Dim f As ADODB.Field
Dim Msg As String
Dim dbInputDatabase As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim SQLStatement As String

On Error GoTo ErrHandler

SQLStatement = "SELECT * FROM InputData WHERE DesignID = " & DesignID

Screen.MousePointer = vbHourglass

dbInputDatabase.Open (ProviderString & ";Data Source=" & InputDatabase)
rs.Open SQLStatement, dbInputDatabase, adOpenKeyset, adLockOptimistic

For Each f In rs.Fields
For Each CurrentControl In Activeform.Controls
If CurrentControl.Tag = f.Name Then
If f.Type = adBoolean Then
CurrentControl = Abs(f.Value)
Else
CurrentControl = f.Value
End If
End If
Next
Next

rs.Close
dbInputDatabase.Close
Set rs = Nothing
Set dbInputDatabase = Nothing

Screen.MousePointer = vbDefault

MsgBox "Design data loaded sucessfully", vbOKOnly + vbInformation, "Load
Design Data"

Exit Sub

ErrHandler:

Screen.MousePointer = vbDefault
Select Case Err
Case Else
Msg = "Unable to load design data. Error Number: " & Err.Number &
vbCrLf & Err.Description
End Select
MsgBox Msg, vbCritical + vbOKOnly, "Load Design Data"

On Error Resume Next

rs.Close
dbInputDatabase.Close
Set rs = Nothing
Set dbInputDatabase = Nothing

End Sub

*****************

Public Sub SaveDesignToDB(Activeform As Form, SaveMode As String)

' This routine saves the contents of all controls on the form that
contain a tag value
' to a the Design database

Dim Msg As String
Dim Style As Integer
Dim Title As String
Dim Response As Integer

Dim CurrentControl As Control
Dim ControlTag As String

Dim dbInputDatabase As New ADODB.Connection
Dim rs As New ADODB.Recordset

Dim SQLStatement As String

On Error GoTo ErrHandler

' If DesignID has been reset, save as new; otherwise, update current
record
If SaveMode = "NEW" Then
Msg = "Save current data as new design in database?"
SQLStatement = "InputData"
Else
Msg = "Overwrite current design in database?"
SQLStatement = "SELECT * FROM InputData WHERE DesignID = " &
CurrentDesignID & ""
End If

Style = vbQuestion + vbOKCancel '+ vbDefaultButton2
Title = "Save Design Data"
Response = MsgBox(Msg, Style, Title)

If Response <> vbOK Then
Exit Sub
End If

Screen.MousePointer = vbHourglass

dbInputDatabase.Open (ProviderString & ";Data Source=" & InputDatabase)
rs.Open SQLStatement, dbInputDatabase, adOpenKeyset, adLockOptimistic

If SaveMode = "NEW" Then
rs.AddNew
End If

For Each CurrentControl In Activeform.Controls
ControlTag = CurrentControl.Tag
If ControlTag <> "" Then
rs.Fields(ControlTag) = CurrentControl
End If
Next
rs.Update
rs.MoveFirst

MsgBox "Design data saved successfully.", vbOKOnly, "Save Design Data"

rs.Close
dbInputDatabase.Close
Set rs = Nothing
Set dbInputDatabase = Nothing

Screen.MousePointer = vbDefault

Exit Sub

ErrHandler:

Screen.MousePointer = vbDefault
Select Case Err
Case Else
Msg = "Unable to save design data. Error Number: " & Err.Number &
vbCrLf & Err.Description
End Select
MsgBox Msg, vbCritical + vbOKOnly, "Save Design Data"

On Error Resume Next

rs.Close
dbInputDatabase.Close
Set rs = Nothing
Set dbInputDatabase = Nothing

End Sub

*****************

Public Sub LoadLists(Activeform As Form, ListFileName As String)

' This routine loads each combo box from the "ListData.dat" file
' in the App.Path\config directory. This is a very fast, easy way
' of maintaining combo lists.

' This function works by searching the main form for a control with an
' assigned tag property that matches a tag entered in the listdata file.
' If one is found, it's list is populated with the values following the
tag
' entry.

Dim FileHandle As Integer
Dim FileName As String
Dim TextInput As String
Dim ItemToAdd As String
Dim Counter As Integer
Dim Msg As String

Dim ControlTag As String
Dim DefaultListIndex As Integer
Dim CurrentControl As Control

On Error GoTo ErrHandler

FileHandle = FreeFile
FileName = App.Path & "\config\ListData.dat"

' Load lists for combo boxes
Open FileName For Input As #1
Do Until EOF(FileHandle)
Line Input #FileHandle, TextInput
If UCase(TextInput) = "CONTROL" Then
ItemToAdd = ""
Input #FileHandle, ControlTag, DefaultListIndex
For Each CurrentControl In Activeform.Controls
If CurrentControl.Tag = ControlTag Then
Do Until UCase(ItemToAdd) = "EOL"
Line Input #FileHandle, ItemToAdd
If UCase(ItemToAdd) = "EOL" Then Exit Do
CurrentControl.AddItem ItemToAdd
Loop
CurrentControl.ListIndex = DefaultListIndex
Exit For
End If
Next
End If
Loop
Close #FileHandle

Exit Sub

ErrHandler:

Close #FileHandle
Screen.MousePointer = vbDefault
Select Case Err
Case Else
Msg = "Error in ListData.dat file. " & Err.Description & "."
End Select
MsgBox Msg, vbCritical + vbOKOnly, "Load List Controls"

End Sub

*****************

Sample ListData.dat file (greatly shortened):

*****************

Combo/List control data file for Visual Basic projects.
Only the lines following the word "CONTROL" and up to "EOL"
are read. The format is as follows:

CONTROL
control tag, default list index
list data 1
list data 2
....
....
EOL

-----------------------------------------------

----State----
CONTROL
"State",0
AL
AK
AZ
AR
CA
WV
WI
WY
EOL

----Nominal OD----
CONTROL
"NominalOD",18
3.00
4.00
4.13
4.25
48.00
EOL

----Bore Dia----
CONTROL
"BoreDia",5
1.13
1.50
2.00
EOL

*****************

Enjoy!

Calan


Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.