Skip to main content
gamedev.net
🔒 Locked

Quick VB Q's

Started by gamechampionx Jul 21, 2002 at 9:42 AM 5 replies 250+ views
Original Post
gamechampionx
gamechampionx
I have a couple quick VB questions for a program I''m making. 1. I am trying to make a message box with vbYesNoCancel, and vbQuestion. How is it done? I tried: MsgBox "Would you like to save before exiting?", vbYesNoCancel, "Save?", , vbQuestion but it crashes when run. 2. How do you tell if a certain file on your hard drive exists? Please help!
Fma
Fma
hmmm....
lets see, first question:
MsgBox "Would you like to save before exiting?", vbYesNoCancel, "Save?", , vbQuestion

if context is provided, helpfile must also be provided(ie. ither remove the vbQuestion parameter or specify a context to go with it.

question 2:
Try this

on error resume next '' desable error messages.

open "Filename" for input as #1
close#1
if err.number=53 then
''file does not exist
end if
on error goto 0 '' enable error messages.
munkie
munkie
To get the returned action of a message box, try this (it's been a while in the VB department..)


      
Dim nMsg as Integer
nMsg = MsgBox("Save before exiting?", vbYesNoCancel + vbSystemModal + vbQuestion, "Save?")

Select Case nMsg
Case vbYes
..
Case vbNo
etc..
End Select


You can add parameters inside the MsgBox statement with the + operator, to specify the type of Message Box. 'vbSystemModal' makes the message box's window stay "on top" (keeps the focus) until you click a button on it.

As for seeing if a file exists on the HD, this should work:


        
If Trim(Dir("c:\whatever\whatever.bmp")) <> "" Then
//file exists

Else
//file does not exist

End If


[edited by - munkie on July 21, 2002 11:32:28 AM]
gamechampionx
gamechampionx
OK, thanks. Also, I''m having a hell of a time accessing public variables. I have a public bas module that reads:


Public Sub Variables()
Dim EquipIDVar(1 To 10) As String
Dim SubSystemVar(1 To 10) As String
Dim DescriptionVar(1 To 10) As String
Dim LocationVar(1 To 10) As String
Dim SchedDateVar(1 To 10) As String
End Sub


and a form with code that reads:


Private Sub Form_Load()
Variables
End Sub

Private Sub Save_Click()
End Sub


Now, in Save_Click(), if I try something like EquipIDVar(1) = "whatever", I get an error message. Why?

Also, on that form I have an input box for each member of each array. Is there a loop I can use to assign all values of an array at once?

Eg:


for i = 1 To 10
EquipIDVar(i) = EquipID & i & .Text
Next


Please help!!

BTW, should I indent more in a sub like:

Private Sub Command1_Click
''Code
End Sub

So it''s like:

Private Sub Command1_Click
''Code
End Sub

???
Cyberdrek
Cyberdrek
quote:
Original post by gamechampionx
OK, thanks. Also, I''m having a hell of a time accessing public variables. I have a public bas module that reads:


Public Sub Variables()
Dim EquipIDVar(1 To 10) As String
Dim SubSystemVar(1 To 10) As String
Dim DescriptionVar(1 To 10) As String
Dim LocationVar(1 To 10) As String
Dim SchedDateVar(1 To 10) As String
End Sub





As for this one, try replacing Dim by Public... IF you Dim your arrays in a .bas module, they are global but only for that module. I had the same problem with a school project...




"DaHjajmajQa''jajHeghmeH!"

Cyberdrek
danielc@iquebec.com
Founder
Laval Linux

/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash
[Cyberdrek | ]
krez
krez
to tell if a file exists, try to open it in input mode... if the file does not exist, you will get an error (which you can handle)... i always use the following as my standard function:

  
Public Function DoesFileExist(FileName As String) As Boolean
On Error Goto DFEERROR
Dim fNUM As Integer
fNUM = FreeFile()
Open FileName For Input As fNUM
Close fNUM
DoesFileExist = TRUE
Exit Function
DFEERROR:
End Function

if there is an error (i.e. the file does not exist), the program will jump to the DFEERROR label, and exit the function (which returns FALSE by default)... if the file DOES exist, it will open and close it, and then return TRUE.

about your public variables:
since you are declaring them in a Sub, they will go out of scope and disappear when that Sub is over... put them at the top of the module, not inside a function/sub... and declare them Global.

HTH...

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
ragonastick
ragonastick
Consider this function instead:


Public Function DoesFileExist(FileName As String) As Boolean

DoesFileExist = IIf(Dir$(FileName) = "", False, True)

End Function


Usually I hate the IIf function, but recently I''ve been using it a fair bit because it is so much fun . Use a straight ''If'' if you really want.

This code:

Public Sub Variables()
Dim EquipIDVar(1 To 10) As String
Dim SubSystemVar(1 To 10) As String
...
End Sub


Is a Public sub containing private variables. Although the Sub can be called from anywhere, the variables in it are only valid from inside that Sub. To create public variables, firstly, consider whether you can avoid making them public (becuase it is usually a sign of bad coding design), then put them at the very top of your code, in the General-Declarations section. If you only need them at module level, then use Dim, if they are public (can be used from other modules/forms) then use Public instead of Dim.

Trying is the first step towards failure.
Trying is the first step towards failure.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.