Shoutbox

Need VB help(Visual Basic) - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Need VB help(Visual Basic) (/showthread.php?tid=49302)

Need VB help(Visual Basic) by dylan! on 08-22-2005 at 07:26 AM

well i just started learning VB the other day and im starting on little projects and im on a cd player thing im making using a guideline kinda of and i need you guys to help me get it to work:P

code:
Private Sub Combo1_Change()
' to determine file type

If ListIndex = 0 Then
File1.Pattern = ("*.wma")
ElseIf ListIndex = 1 Then
File1.Pattern = ("*.mp3")
Else
Fiel1.Pattern = ("*.*")
End If

End Sub


Private Sub Dir1_Change()

'To change directories and subdirectories(or folders and subfolders)


File1.Path = Dir1.Path
If Combo1.ListIndex = 0 Then
File1.Pattern = ("*.wma")
ElseIf Combo1.ListIndex = 1 Then
File1.Pattern = ("*.mp3")
Else
File1.Pattern = ("*.*")
End If
End Sub

Private Sub Drive1_Change()

'To change drives

Dir1.Path = Drive1.Drive

End Sub

Private Sub File1_Click()
If Combo1.ListIndex = 0 Then
File1.Pattern = ("*.wma")
ElseIf Combo1.ListIndex = 1 Then
File1.Pattern = ("*.mp3")
Else
File1.Pattern = ("*.*")
End If

If Right(File1.Path, 1) <> "\" Then
filenam = File1.Path + "\" + File1.FileName
Else
filenam = File1.Path + File1.FileName
End If
Text1.Text = filenam

End Sub


Private Sub Form_Load()

'To center the Audioplayer startup page


Left = (Screen.Width - Width) \ 2
Top = (Screen.Height - Height) \ 2
Combo1.Text = "*.wav"
Combo1.AddItem "*.wma"
Combo1.AddItem "*.mp3"
Combo1.AddItem "All files"

End Sub

Private Sub AudioPlayer_Click()

End Sub


Private Sub Command1_Click()

'To play Windows Media Player file or MPEG layer 3 Audio


If Combo1.ListIndex = 0 Then
AudioPlayer.DeviceType = ".wma"
ElseIf Combo1.ListIndex = 1 Then
AudioPlayer.DeviceType = ".mp3"
End If
AudioPlayer.FileName = Text1.Text
AudioPlayer.Command = "Open"
AudioPlayer.Command = "Play"

End Sub


Private Sub Command2_Click()
If AudioPlayer.Mode = 524 Then Exit Sub
If AudioPlayer.Mode <> 525 Then
AudioPlayer.Wait = True
AudioPlayer.Command = "Stop"
End If
AudioPlayer.Wait = True
AudioPlayer.Command = "Close"
End Sub

Private Sub Command3_Click()
End
End Sub


the part that it says is invalid or w.e that wont make it work is the BOLD text near the top about the file types
RE: Need VB help(Visual Basic) by segosa on 08-22-2005 at 07:33 AM

File1.Pattern = "*.wma"

etc


RE: Need VB help(Visual Basic) by dylan! on 08-22-2005 at 07:34 AM

thats what i have?:S:S


RE: Need VB help(Visual Basic) by Dempsey on 08-22-2005 at 07:43 AM

without brackets I think Seggeh means


RE: Need VB help(Visual Basic) by dylan! on 08-22-2005 at 07:48 AM

now it when i start my .mp3 files it says theres and error with

code:
AudioPlayer.DeviceType = (".mp3")
:S:Sand i assume if i search for .wma the same problem will occure for the otehr line


heres a screeny of what i have and if you wanna try and make it and get it to work i would appreciate it:D
[Image: attachment.php?pid=517813]
RE: Need VB help(Visual Basic) by Dempsey on 08-22-2005 at 07:50 AM

remove the brackets too

quote:
AudioPlayer.DeviceType = ".mp3"

RE: Need VB help(Visual Basic) by dylan! on 08-22-2005 at 07:55 AM

:@:@:@:S:S now i removed the bbrackets and it says theres an error with the code

code:
AudioPlayer.Command = "Open"
:@:@:S
RE: Need VB help(Visual Basic) by spokes on 08-22-2005 at 08:26 AM

hmm, just wondering, are you using media player control?

also, i suggest u put variations in the file pattern thing

because it wont find .MP3 or .WMA files otherwise, some songs have upper case extensions

example

File1.Pattern = "*.mp3;*.MP3"

etc...

what error is it giving you wivout the brackets?


RE: Need VB help(Visual Basic) by dylan! on 08-22-2005 at 08:30 AM

runtime error '424':
Object required

and the code that has teh error is

code:
AudioPlayer.DeviceType = ".mp3"

and im using Microsoft Multimedia Control 6.0

its just set to invisible
RE: Need VB help(Visual Basic) by spokes on 08-22-2005 at 08:36 AM

hmm, i'm sorry, i can't help you with that, i always use the media player control


www.pscode.com might have something useful


RE: Need VB help(Visual Basic) by dylan! on 08-22-2005 at 08:39 AM

where can i get this "media player control"?:P


RE: Need VB help(Visual Basic) by spokes on 08-22-2005 at 08:47 AM

in an open VB window, press ctrl + t (project - components)

and click browse, and goto your system folder and select msdxm.ocx


RE: Need VB help(Visual Basic) by dylan! on 08-22-2005 at 09:13 AM

ok...im not worrying about that anymore but now i need to know if theres a quick code to make it enter
e.g
Dim message As Interger

message= "Hello World!"
print (pagedont/enter here)message

and it would normally just print
" Hello World"
but i want it to print
"(pagedown/enter in affect here!!!)
Hello World"

and yeah i was wondering what i could put to make it like that?


RE: Need VB help(Visual Basic) by spokes on 08-22-2005 at 09:22 AM

i don't get what you mean :S


RE: Need VB help(Visual Basic) by dylan! on 08-22-2005 at 09:24 AM

like where it would normally say
"Hello World"
it leaves a blank line before it says it like this:
"
Hello World"

just like you press enter in the box where u reply to me it goes down a line but i need this in vb:P


RE: Need VB help(Visual Basic) by spokes on 08-22-2005 at 09:33 AM

print vbnewline & message

:p


RE: Need VB help(Visual Basic) by dylan! on 08-22-2005 at 09:48 AM

ok...ahah...ONE LAST THING!!:P...ok se heres my code

code:
Private Sub Command1_Click()
Dim intMsg As String
Dim Message As String

Open "C:\Documents and Settings\!~DyLaN~!\Desktop\VB\sample.txt" For Output As #1
intMsg = MsgBox("File sample.txt opened")
Message = InputBox("Enter Message")
Print #1, vbNewLine; Message
intMsg = MsgBox("Writin " & StudentName & " to sample.txt ")

Close #1

End Sub


Private Sub Form_Load()
MsgBox "Hello"

End Sub

and i want to give that to people so they can send me messages but whenever someone writes a message it saves over the last one and i was wondering how i could get it to just keep adding on to sample.txt

here it is if u wanna send me a message:P
RE: Need VB help(Visual Basic) by spokes on 08-22-2005 at 10:01 AM

Open "C:\Documents and Settings\!~DyLaN~!\Desktop\VB\sample.txt" For Append As #1


RE: Need VB help(Visual Basic) by spokes on 08-22-2005 at 10:10 AM

path not found, lol

this isn't going to work, unless we r on your pc

you should learn some winsock, make a server, listenin for messages, and make a client which sends the messages :p


RE: Need VB help(Visual Basic) by CookieRevised on 08-22-2005 at 11:05 AM

dylan!, I suggest you first try some basic VB samples and coding, what you're asking for is more then you can handle (atm), even the media player is much more difficult then you think.

Grab some beginners book for VB, or at least learn to use the help files which come with VB (Press F1 very regulary and almost with each command you type, property you use, etc. to know exactly what it means and does). Online tutorials are also good to learn. But stick with the basics, and until you master that try some more difficult things gradually.

Btw, the send message stuff you wanted to do is very advanced and shouldn't be attempted until you are very experienced in VB; it is of nu use that we answer all your single questions, as that would make this thread 6541641 pages long because you will encounter error after error and problem after problem

This is not to put you down or something, but you don't run when you can't walk... You need to start at the beginning, not somewhere in the middle or even beyond that ;)


To illustrate:

code:
Private Sub Form_Load()
  Left = (Screen.Width - Width) \ 2
  Top = (Screen.Height - Height) \ 2
End Sub
1) Why do you use the properties like "Left", "Width" and not "Form.Left", "Form.Width", etc?
2) Why do you use "\" and not "/"?

code:
File1.Pattern = ("*.wma")
1) What is the meaning in VB of the brackets if you use them like that?
2) What is use of the .Pattern property and how to you use it exactly?

code:
Private Sub Command2_Click()
  If AudioPlayer.Mode = 524 Then Exit Sub
  If AudioPlayer.Mode <> 525 Then
  AudioPlayer.Wait = True
  AudioPlayer.Command = "Stop"
  End If
  AudioPlayer.Wait = True
  AudioPlayer.Command = "Close"
  End Sub
1) Put all that in a decent IF.. THEN...ELSE structure
2) Put all that in a SELECT CASE structure....


All these questions are basic VB stuff to know....

Happy learning and experimenting
RE: Need VB help(Visual Basic) by spokes on 08-22-2005 at 11:13 AM

quote:
Originally posted by CookieRevised
    code:Private Sub Form_Load()
      Left = (Screen.Width - Width) \ 2
      Top = (Screen.Height - Height) \ 2
    End Sub

1) Why do you use the properties like "Left", "Width" and not "Form.Left", "Form.Width", etc?
2) Why do you use "\" and not "/"?

Why don't you use / ? :$


RE: Need VB help(Visual Basic) by CookieRevised on 08-22-2005 at 11:44 AM

Visual basic knows 2 different divisions.
One is for integers, one is for floating points.

"/" is the floating point devision. If you use that, then all variables used in the expression are first converted to floating points, then they are devided and the result will be again a floating point.

"\" is the integer devision sign. Before the expression is calculated, all the variables will be rounded down to the nearest integer (Byte, Integer, or Long) first. The result will again be an integer (and thus rounded down also).

eg:
5% / 2% = 5! / 2! = 2.5!
5! \ 2% = 5% \ 2% = 2%
89 \ 9 = 9 (and not 10; aka it is rounded down to the nearest integer, the .5 rule doesn't apply here as fractions can't exist in integers)

"%" is the marker for an 'integer'-type, "!" for a 'single'-type (aka small floating point number)

The integer devision is extremely faster than the floating point devision.

You can find this also under the topic "Arithmetic Operators" in your VB help files.

Too know exactly what integers, singles, longs, etc are look at the "Data Type Summary" topic in the help files.

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

Knowing the above, doesn't completely answer the question though.

The .Left, .Right, .Width, etc. properties of a form are always expressed in Twips*. Twips are always whole numbers, so you can't have something like 654.31 twips (unlike inches or centimeters which can be fractions)... Thus when you calculate the middle of the screen by deviding by 2, the solution must always be rounded to the nearest full unit anyways. So you better use integer devision for faster calculation (unless you want to use the .5 rule).

Left = (Screen.Width - Width) / 2
could have been used also of course. In that way VB will do actually the same, but the calculation would have been taken longer because VB needed to convert all the variables to floating points first and then calculate with floating point to end with dismissing the fraction in the end anyways.

;)


* a twip is a screen-independent, absolute unit of measurement. A twip is a unit of length equal to 1/20 of a printer's point, and a printer's point is 1/72 of an inch. There are approximately 1440 twips to a logical inch or 567 twips to a logical centimeter (the length of a screen item measuring one inch or one centimeter when printed). Other means of measurement include characters (not to be confused with the term "a character"), pixels (the smallest possible unit) and himetrics (unless you do very advanced stuff in VB you would never encounter or use himetrics though).

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

All this info is also available in the VB help files ;)


RE: Need VB help(Visual Basic) by dylan! on 08-25-2005 at 12:49 AM

ok ive study lots in the last couple of days and if learnt wuite a bit:P and i think ive read over 50 pages of tutorials on it on the internet:P and i was just wondering one small and simple question
ok I want to create a folder and file on the click of a button but i was wondering what i would put to create it?????
e.g
Create "C:\Program Files\Program\Info.txt"
but i know create isnt a valid command and i was wondering what i could put to create it


RE: Need VB help(Visual Basic) by matty on 08-25-2005 at 01:07 AM

I sure hope you understand this
Checks if a folder exists on the drive

code:
Private Sub Form_Load()
    DoesFolderExist "C:\My Test Folder", True
End Sub

Public Function
DoesFolderExist(strPath As String, Optional blnCreateFolder As Boolean = False) As Boolean
On Error GoTo e:
    ChDir strPath
    DoesFolderExist = True
    Exit Function

e:
    If blnCreateFolder = True Then
        MkDir strPath
    End If
    DoesFolderExist = False
End Function

Checks if a file exists and it not creates it
code:
Private Sub Form_Load()
    DoesFileExist "C:\My Test Folder\test.txt", True
End Sub

Public Function
DoesFileExist(strFile As String, Optional blnCreateFile As Boolean = False) As Boolean
On Error GoTo
e:
    FileLen (strFile)
    DoesFileExist = True
    Exit Function

e:
    If blnCreateFile = True Then
        Open
strFile For Append As #1
        Close #1
    End If
    DoesFileExist = False
End Function


RE: Need VB help(Visual Basic) by CookieRevised on 08-25-2005 at 03:49 AM

:$ I feel bad about this.... but.... Matty, use the Dir() function to check if a folder or file exists so you don't need to use error checking* or changing your application path ;) :$

* it still isn't totally 100% fail proof of course, but you know what I mean ;)

code:
Public Function DoesFolderExist(strPath As String, Optional blnCreateFolder As Boolean = False) As Boolean
    Dim AnyFolder As Long
    AnyFolder = vbDirectory Or vbReadOnly Or vbHidden Or vbSystem Or vbArchive Or vbNormal
 
    If Dir(strPath, AnyFolder) <> "" Then
        DoesFolderExist = True
    Else
        If blnCreateFolder Then
            MkDir strPath
        End If
        DoesFolderExist = False
    End If
End Function


Public Function DoesFileExist(strFile As String, Optional blnCreateFile As Boolean = False) As Boolean
    Dim AnyFile As Long
    AnyFile = vbReadOnly Or vbHidden Or vbSystem Or vbArchive Or vbNormal
 
    If Dir(strFile, AnyFile) <> "" Then
        DoesFileExist = True
    Else
        If blnCreateFile Then
            Open strFile For Output As #1
            Close #1
        End If
        DoesFileExist = False
    End If
End Function


IMPORTANT: I suddenly remembered why I never used Dir() for this myself (but the windows API's instead): There is a small, but important, 'twitch' in the Dir() function and the vbDirectory attribute usage (this goes for many programming languages btw, not only VB). Hence, the above "DoesFolderExist" procedure isn't 100% "bug" free and the ones below should be used instead. Sorry for this late, but important, addition...

Because of the nature of the attributes of files and directories in Windows, there can be false positives returned when searching for a directory. eg: Dir() will also return a file name (if the name matches of course), even if the vbDirectory attribute is given as a mandatory attribute to have.

To overcome this, you can make sure that the given path ends with "\". If the path exists the Dir() function will return the special file "." (meaning current directory), if not it will return an empty string "", and no false positive will be given anymore.

BUT! by adding the "\" at the end, you actually force Dir() to use the given variable as 'a path name'. If there is that file again with the same name as the last subdir in that path you wanna check, it will not produce a false positive anymore, but a runtime error 52. This is because there was a name conflict when looking up the path (which thus actually wasn't a valid path for Windows)...

Thus, to compensate for this, we need to understand how files on a computer work and how to use "special" filenames. To make it short (you can read detailed stuff about this on the net), you can/should use the special filename "NUL".

With this, we can check for the NUL-file/path. An added bonus to this is that we don't need to specify the other attributes anymore (except for vbDirectory of course) and that invalid characters (like , ; : / etc.) don't trigger an error either!

We still need to check for false positives though, but the name conflicts will not occur anymore...

code:
' "Byval" because we are going to change the variable,
' and we don't want the original passing variable to
' change with it...

Function DoesFolderExist(ByVal strPath As String) As Boolean
      ' Set the default returning value
      DoesFolderExist = False
   
      ' Make sure we start with a clean file/path name
      If Right$(strPath, 1) = "\" Then strPath = Left$(strPath, Len(strPath) - 1)
   
      ' Check for a matching name
      If Dir(strPath & "\nul", vbDirectory) = "nul" Then
          ' Compensate for false positives (matching file names)
          If GetAttr(strPath) And vbDirectory Then
              ' We truely have an existing directory at our hand
              DoesFolderExist = True
          End If
      End If   
End Function
Second important note: I left out the creation of the directory because it also isn't as strait forward as simply stating MkDir strPath. For starters, each dir above the last subdir must exist before you can create that last subdit. eg: if "C:\MyDir" doesn't exist, you can not do MkDir "C:\MyDir\SubDir".


Last note: simply use the Windows API, life is much easier (and in this case faster and shorter) with that...


PS: The "DoesFileExist" procedure doesn't suffer from all this because, vice versa, files don't have the vbDirectory attribute in the first place and thus a directory will never be returned, even if the name matches a directory! But again, creating a file isn't as strait forward as it is originally stated; there are lots of errors which could occur. And therefore, the DoesFileExist() function can be reduced to (following the same NUL-usage to compensate for bad characters):
code:
Public Function DoesFileExist(strFile As String) As Boolean
  If Dir(strFile & "\nul") = "nul" Then
    DoesFileExist = (GetAttr(strFile) And (vbDirectory Or vbVolume)) = 0
  End If
End Function


Personal note: the usage of the NUL-file/path in these ways is something I nowhere found on the net, it isn't even mentionned on so called prof, advanced or hardcore VB sites. Instead all you find are half-arsed (excuse for the wording) copy/pasted sources. I wonder how many actually know about this :/



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

Anyways, to come back to the subject.... :P


quote:
Originally posted by dylan!
ok ive study lots in the last couple of days and if learnt wuite a bit:P and i think ive read over 50 pages of tutorials on it on the internet:P and i was just wondering one small and simple question
ok I want to create a folder and file on the click of a button but i was wondering what i would put to create it?????
e.g
Create "C:\Program Files\Program\Info.txt"
but i know create isnt a valid command and i was wondering what i could put to create it

it isn't so simply though.... There are lots of things to do and to keep in mind when doing something like that.

* What do you want to do if the file already exists? Overwrite it? Append to it?
* What do you want to do when the directory (or subdirectories) don't exist? Create them?
* What if the file and/or directories do exist, but can't be overwritten?
* What kind of data are you going to write to the file? Binary? Text?
* Do you want the file to be locked for writing by another process while you are writing to it?
* Do you want the file to be locked for reading by another process while you are writing to it?
* etc...

But basically, disregarding all those questions it goes like:

code:
Open "C:\Program Files\Program\Info.txt" For Output As #1
    Print #1, "some line"
    Print #1, "some other line"
    Print #1, "more lines"
Close #1
This creates/overwrites the file, without locking it, and write ascii text to it, while assuming the directory exists and is accessible and without checking for disk quota or other errors that might occur.


As said before, use your Visual Basic Documentation which is installed when you installed Visual Basic by pressing <F1>.... Or you can lookup that same documentation online by going to the MSDN Library:
http://msdn.microsoft.com/library/default.asp?url...html/vb6anchor.asp


eg:
The specific docs for the used statements and commands in the above example:
* Open statement
* Print # statement
* Close statement


Learn to use the Visual Basic documentation and/or MSDN Library ;)

RE: Need VB help(Visual Basic) by dylan! on 08-25-2005 at 10:50 PM

thanks cookie again;):P but when i click F1 it says it couldnt find and mds library or something8-)


RE: Need VB help(Visual Basic) by CookieRevised on 08-25-2005 at 11:53 PM

quote:
Originally posted by dylan!
thanks cookie again;):P but when i click F1 it says it couldnt find and mds library or something8-)
You don't happen to have an illegal VB version don't you? Anyways you can also use the msdn library for vb on the net...

more specifically The Visual Basic Document: Programmers Reference. There you'll find every command, statement, whatever you can use with full detailed explanations. eg: see my previous post and the links to the "Open", "Print #" and "Close" statement.



RE: Need VB help(Visual Basic) by dylan! on 09-11-2005 at 08:18 AM

no i wouldnt be using an illegal version8-)....anyways ive done a couple little things already...calculators and stuff but im stuck...i need to take text from a .txt file and put it into a msgbox

code:
Private Sub Command2_Click()
Open "C:\Program Files\Multi-app\password.txt" For Output As #1
Pass = "C:\Program Files\Multi-app\password.txt"
MsgBox (#1 & " is your Password.")
Close #1
End Sub

BUT....theres an error on the "MsgBox (#1 & " is your Password.")" line with the #1 and i was wondering what i could do to fix that8-)
RE: Need VB help(Visual Basic) by spokes on 09-11-2005 at 08:32 AM

code:
Private Sub Command2_Click()
Open "C:\Program Files\Multi-app\password.txt" For Input As #1
line input #1, pass
MsgBox (pass & " is your Password.")
Close #1
End Sub

RE: Need VB help(Visual Basic) by dylan! on 09-11-2005 at 09:03 AM

still doesnt work heres my complete code8-)

code:
Dim intMsg, Message As String
Private Sub Command2_Click()
Open "C:\Program Files\Multi-app\password.txt" For Input As #1
Line Input #1, Pass
MsgBox (Pass & " is your Password.")
Close #1
End Sub
Private Sub Command3_Click()
Open "C:\Program Files\Multi-app\password.txt" For Append As #1
intMsg = MsgBox("Password Rememberer opened.")
Message = InputBox("Enter Password")
Print #1, intMsg; Message

Close #1
End Sub

Private Sub Command1_Click()
Dim Pass As Integer
Open App.Path & "C:\Program Files\Multi-app\password.txt" For Input As #1
Pass = InputBox("Enter Password.")
MsgBox (Pass & " remembered.")

End Sub

Public Function DoesFolderExist(strPath As String, Optional blnCreateFolder As Boolean = False) As Boolean
    Dim AnyFolder As Long
    AnyFolder = vbDirectory Or vbReadOnly Or vbHidden Or vbSystem Or vbArchive Or vbNormal
 
    If Dir(strPath, AnyFolder) <> "" Then
        DoesFolderExist = True
    Else
        If blnCreateFolder Then
            MkDir strPath
        End If
        DoesFolderExist = False
    End If
End Function


Public Function DoesFileExist(strFile As String, Optional blnCreateFile As Boolean = False) As Boolean
    Dim AnyFile As Long
    AnyFile = vbReadOnly Or vbHidden Or vbSystem Or vbArchive Or vbNormal
 
    If Dir(strFile, AnyFile) <> "" Then
        DoesFileExist = True
    Else
        If blnCreateFile Then
            Open strFile For Output As #1
            Close #1
        End If
        DoesFileExist = False
    End If
End Function

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  If MsgBox("Are you sure you want to shut down?", vbYesNoCancel) <> vbYes Then
    Cancel = True
  End If
End Sub



Private Sub mnuHelp_Click(Index As Integer)
MsgBox "Please email me at lucky_number_15s@hotmail.com for any help or SUGGESTIONS!"
End Sub

RE: Need VB help(Visual Basic) by segosa on 09-11-2005 at 10:12 AM

code:
Open App.Path & "C:\Program Files\Multi-app\password.txt" For Input As #1


Do you even KNOW what App.Path is? Surprise surprise, it's the path to the application. Why are you appending another path to the end?!

Why don't you re-read CookieRevised's post?

If you don't know a language you're never going to learn it by randomly copying/pasting code from other places hoping it'll work, and then asking other people to fix your code without an explanation of what was wrong. Buy a book. Follow some tutorials. Learn.
RE: Need VB help(Visual Basic) by ShawnZ on 09-11-2005 at 10:15 AM

quote:
Originally posted by dylan!
Open App.Path & "C:\Program Files\Multi-app\password.txt"

I don't even know VB and I'm wondering why you're concatenating a full path with a full path.

Edit: not to mention forgetting to close it.
RE: Need VB help(Visual Basic) by mad_onion on 09-11-2005 at 09:01 PM

hmm this is all very interesting. im starting vb soon so i hope you will be able to help me with my coursework im making a roman numerals calculator (sounds easy) but i will probably need your help. i currently dont know any so it will be a steep learing curve :)


RE: Need VB help(Visual Basic) by prashker on 09-11-2005 at 09:38 PM

quote:
Originally posted by mad_onion
hmm this is all very interesting. im starting vb soon so i hope you will be able to help me with my coursework im making a roman numerals calculator (sounds easy) but i will probably need your help. i currently dont know any so it will be a steep learing curve

http://www.vb-helper.com/howto_calculator.html

RE: RE: Need VB help(Visual Basic) by segosa on 09-12-2005 at 06:48 AM

quote:
Originally posted by mad_onion
hmm this is all very interesting. im starting vb soon so i hope you will be able to help me with my coursework im making a roman numerals calculator (sounds easy) but i will probably need your help. i currently dont know any so it will be a steep learing curve :)


2 years ago my A/S level computing task was a roman numerals calculator. Not doing Cambridge by any chance are you?
RE: Need VB help(Visual Basic) by mad_onion on 09-12-2005 at 06:15 PM

lol yeah im doing ocr which is cambridge :) i cant believe they dont change the tasks lol :S
what did you get in the end and did you do it to A2 level?


RE: RE: Need VB help(Visual Basic) by CookieRevised on 09-14-2005 at 03:53 AM

quote:
Originally posted by dylan!
still doesnt work heres my complete code 8-)

Please read the links I've posted. Especially those to the online MSDN library. Also read them and understand them. Study (and I mean really: study) the examples. Play with those by altering small stuff, learn from that...

Also, go to your local library and pick up a book like "VB for beginners", "Starting with VB" and the likes! This is no joke!

RED = errors
GREEN = corrected or added code
ITALIC = Look up these terms in the help files (eg: msdn library), look them up on VB sites and read about them in books. Study them! and understand what they are, involve, the possebilities are, when they are used, why they are used, etc...


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

code:
Dim intMsg, Message As String
Your first line is already faulty!

1) For each variable you declare you need to specify the data type. If you don't do this, the variable will be declared as type variant, which is very slow and prone to errors.

2) If you intended to declare intMsg as a string data type (because you put it on the same line?) then that's wrong. intMsg should be a numerical data type as it will recieve the return code from the messagebox.

Correct would be:
code:
Dim intMsg as Integer, Message as String
or
code:
Dim intMsg as Integer
Dim Message as String

Also, understand why you put those variables outside of any procedure and what the implications of that are!!! This is extremely important to understand!!!

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

code:
Private Sub Command2_Click()
Dim Pass As String
Open "C:\Program Files\Multi-app\password.txt" For Input As #1
Line Input #1, Pass
Where is your Pass variable declared? ALWAYS declare variables before using them or they will be declared as the data type variant!! To avoid such errors put "Option Explicit" on top of each module, form, class, whatever...

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

code:
MsgBox (Pass & " is your Password.")
1) Why do you use parenthesis? Understand the reason why you can leave them out here. In fact, you should leave them out here as they, in this context, do not mean "put parenthesis around my used variables when I call this function" because you don't call MsgBox as a function but use it as a procedure/sub.

The parenthesis in this particular context mean "group these instructions together and execute it first before the ones outside this group". Now, there is nothing outside it to execute. This will not result in an error but it will be left out automatically by VB when you compile your code because it is useless... Instead use
code:
MsgBox Pass & " is your Password."
or
code:
Call Msgbox(Pass & " is your Password.")
Study the difference between these two ways of calling a sub (or a function as sub in this case). And study the difference between the use of parenthesis in the last one and with your code as there is a major difference because the parenthesis have yet another total different use there.

To illustrate this:
code:
FirstNumber = 8
SecondNumber = 7
Call DoSomeCalc(FirstNumber, (SecondNumber), 5 * (2 - 1))
All three pairs of parenthesis have three very different functions and do very different things!!!
1) Enclose all variables used to call a function or sub
2) Pass a variable by value instead of by reference
3) Alter the order of executing instructions in an instruction set


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

code:
Private Sub Command3_Click()
Open "C:\Program Files\Multi-app\password.txt" For Append As #1
If you would have read the links I posted to the official help documents of Open, Close, etc you would know why append is not the correct choice here. Append will open the file for appending data to it. This means it will add data to the file, not replace it!! Since you, in Command2_Click(), read out the first line of the file, the added passwords to that file will never be read out!
code:
Private Sub Command3_Click()
Open "C:\Program Files\Multi-app\password.txt" For Output As #1
Again, look up all this stuff and study why, how and when to use what.

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

code:
intMsg = MsgBox("Password Rememberer opened.")
Message = InputBox("Enter Password")
Print #1, intMsg; Message
1) Is there a reason why you also store the return code intMsg of the messagebox??? I don't think so, especially since you want to read out the password, and not the return code.

2) Is there even a reason to catch the return code? You don't do anything with it, so why assigning the return code to the variable intMsg in the first place?

3) Do you know what ";" means in this case? Look it up...

4) Do you know why you use "Print #1" here? What is the meaning of "#1"?
code:
MsgBox "Password Rememberer opened."
Message = InputBox("Enter Password")
Print #1, Message

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

code:
Private Sub Command1_Click()
Dim Pass As Integer
Why integer???? An integer is a numerical data type. The InputBox function returns a string data type, not a numerical one.
code:
Private Sub Command1_Click()
Dim Pass As String

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

code:
Open App.Path & "C:\Program Files\Multi-app\password.txt" For Input As #1
1) What is "App.Path &" doing there??? Even if you don't know what it means, you surely would see that it will add something to the path string you already have and you hopefully would know "somethingblahblahc:\program files\myfile.txt" is not a valid path!

2) App is an object which contains lots of properties and a few methods. One if these properties is Path. As you can read in the helpfiles and all over the net, it contains the application's path; In case you run your application in the debugger it contains the path to VB6.EXE. In case you compile your application it will contain the correct path of where your application (the exe) is stored.

3) About the "For Input". Why input??? You clearly ask for a password and then say "remembered" in the messagebox. Thus, logically and judging from that, you are saving the password. And thus you should open the file for Output, use Print #1 and don't forget to Close the file after writing. In other words, this whole would be exactly the same as Command3_Click().

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

code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  If MsgBox("Are you sure you want to shut down?", vbYesNoCancel) <> vbYes Then
    Cancel = True
  End If
End Sub
Judging from your other code, shown above, I can guess that you didn't wrote this? If so, try to understand what it really does. eg:
-Why are the statements used like they are used
-Why using MsgBox("something", vbYesNoCancel) suddenly? From where does ", vbYesNoCancel" part comes from? What does it mean?
-What is vbYes? What data type is it?
-What is the exact use of Cancel?
-Why do all this in the QueryUnload event?
-What is an event exactly?
-When does QueryUnload trigger exactly? Does it trigger more than once?
-What events proceed and follow the QueryUnload event by default?

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

code:
Private Sub mnuHelp_Click(Index As Integer)
Understand from where the "Index As Integer" comes from, how it got there, etc. (the answer is not "because VB6 put it there", as this is a direct result of something you did when designing the form)



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



I will attach the completed corrected code. But this is for the last time since you realy need to start learning and studying all these things instead of copying and writing code you don't understand.

We can help with some real bugs or questions, but not with teaching someone how to program. We can not do your "homework" for you, you need to do that yourself; there are books for that and they are not for nothing hundreds of pages long. It is impossible to write a complete book here...

Sorry to say all this, but coming here with these kind of questions is not the right place to go. The correct place would be the library or at least the helpfiles which you constantly should use on every statement and piece of code you write to understand why you write it, how you write it and when you write it.



code:
Option Explicit

Private Sub Command2_Click()
  Dim Pass As String
  Open "C:\Program Files\Multi-app\password.txt" For Input As #1
    Line Input #1, Pass
  Close #1
  MsgBox Pass & " is your Password."
End Sub

Private Sub Command3_Click()
  Dim Pass As String
  Pass = InputBox("Enter Password:")
  Open "C:\Program Files\Multi-app\password.txt" For Output As #1
    Print #1, Pass
  Close #1
  MsgBox "Password saved."
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  If MsgBox("Sure?", vbYesNoCancel) <> vbYes Then
    Cancel = True
  End If
End Sub

Private Sub mnuHelp_Click(Index As Integer)
  MsgBox "Mail me at here@blah.com"
End Sub

Public Function DoesFolderExist(ByVal strPath As String) As Boolean
  DoesFolderExist = False
  If Right$(strPath, 1) = "\" Then strPath = Left$(strPath, Len(strPath) - 1)
  If Dir(strPath & "\nul", vbDirectory) = "nul" Then
    DoesFolderExist = GetAttr(strPath) And vbDirectory
  End If
End Function

Public Function DoesFileExist(strFile As String) As Boolean
  If Dir(strFile & "\nul") = "nul" Then
    DoesFileExist = (GetAttr(strFile) And (vbDirectory Or vbVolume)) = 0
  End If
End Function



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



Look up EVERY WORD that is part of the VB language in your code (aka every word which isn't made by you)!!! READ the entire explaination of it and STUDY the examples given about them in books and official help files and/or msdn library... This seems like a lot, but it is essential that you fully understand what each and every single command (yes, even symbols) means and does...

Learn to walk before you want to run... ;)



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

EDIT: I've also updated my first post in this thread with some important info concearning the DoesFolderExist() and DoesFileExist() routines... sorry for being so late with this, but I handn't the time earlier to write all that...

quote:
Originally posted by SonicSam
quote:
Originally posted by mad_onion
hmm this is all very interesting. im starting vb soon so i hope you will be able to help me with my coursework im making a roman numerals calculator (sounds easy) but i will probably need your help. i currently dont know any so it will be a steep learing curve
http://www.vb-helper.com/howto_calculator.html
which contains (obvious) bugs (as many of their sources do btw)

RE: Need VB help(Visual Basic) by dylan! on 10-09-2005 at 05:57 AM

hokay...i need some help...i have a thinger...and i have a filelist box thinger and i was wondering if when you clicked on a file in the filelist box thinger could it chnage the OLE to the selected file? and how?:S  and even more so if when you click on files on the OLE if it was a movie file or media file if it could play in a little wmp thinger? and how?:SOR even more so...when you click on a file in the filelistbox thinger it would play in the little wmp thinger? and how?:S


RE: Need VB help(Visual Basic) by CookieRevised on 10-09-2005 at 01:14 PM

Look at the various events available for the filelistbox...

eg:
File1_Click()
File1_KeyDown()
File1_KeyPress()
File1_PathChange()
File1_PatternChange()
etc...

Use the appropiate event for your cause and use the appropiate properties of the FileListBox to get the current selection. Handle it from there...


RE: Need VB help(Visual Basic) by dylan! on 10-09-2005 at 10:36 PM

ok...ive looked but i still dont undertsnad this...there is nothing about what i need or want in my book...i need help with the code from getting the the selected in the filelistbox to play in wmp control??


RE: Need VB help(Visual Basic) by Ezra on 10-09-2005 at 11:17 PM

isn't it something like filelistbox1.selected.index ?


RE: Need VB help(Visual Basic) by dylan! on 10-09-2005 at 11:24 PM

i dont know:S thats why i need help...i have a list of files in a filelistbox and when u double click on one it plays in the wmp control??


RE: Need VB help(Visual Basic) by surfichris on 10-09-2005 at 11:50 PM

I can't remember exactly, and don't have VB on the computer i'm currently using, but isn't it something like:

SelectedIndex contains the index of the current selected item - this will be an integer starting from zero.

To get the text of the selected item can't you use this?

Dim MyItem as String
MyItem = List1.Items(List1.SelectedIndex)


RE: Need VB help(Visual Basic) by dylan! on 10-09-2005 at 11:53 PM

i dont know:S i dont know anything at all about this:S it was it my book and i cant find any online tutorials on it...


RE: Need VB help(Visual Basic) by CookieRevised on 10-10-2005 at 12:13 AM

quote:
Originally posted by Chris Boulton
To get the text of the selected item can't you use this?
Dim MyItem as String
MyItem = List1.Items(List1.SelectedIndex)
Yep, only it is "ListIndex". "SelectedIndex" is from VB.NET...

quote:
Originally posted by dylan!
and i cant find any online tutorials on it...
ermmm.... google "vb filelistbox"... How many tuts do you want?? The net is bulking with simple examples like you want, all you need to do is clicking, reading and learning...

quote:
Originally posted by dylan!
i dont know:S i dont know anything at all about this:S it was it my book and i cant find any online tutorials on it...
I pointed you earlier to the msdn library... Look up the FileListBox there and read about its properties like "ListIndex".

I'm sure even in your book there is info on how to get a current selected item of a list as this is something very basic to know.

And in the VB IDE if you type the name of the FileListBox control and put a point after it (to enter one of its properties) you also get a list of the available properties. Look them up!

PS: if you have a hard time doing VB stuff, I really really really suggest to get to the library and rent some beginners book and read it from the start and don't jump to things before you understand for 500% the previous pages. And thus follow each given example to the letter and experiment with it until you fully understand everything before you go to the next page...

Start by understanding and learning how to use the VB IDE (eg: to get a list of properties of controls, etc...) before you start to program.

;)
RE: Need VB help(Visual Basic) by dylan! on 10-10-2005 at 07:07 PM

yeah ok i got books form the library...2...2 HUGE books ive been reading for a month...BUT a new problem has evolved...i dont got the Play alternative or w.e its called but i have stop pause ect...


RE: Need VB help(Visual Basic) by Ezra on 10-10-2005 at 07:31 PM

Why don't you stop the project you are working on and start simple things, like manipulating variables and stuff... You have to understand why things work certain ways, and if you just keep asking here you will  know what to do, but not why.

Start with simple things, learn how to use a simple listbox after that you can make it more difficult.

I'm not trying to piss you off or anything like that, i'm just saying as cookie has said many times, start with the easy stuff, learn why things behave like they do, and work onwards from that.

I myselve just started learning VB.NET and i'm making dull programs that open bmp's and change simple variables, but eventually i'll be abled to make bigger and more advanced programs.

Just take it one step at a time, and you'll learn much quicker and better


RE: Need VB help(Visual Basic) by dylan! on 10-22-2005 at 05:49 AM

ok so im back...this time its not the simple problems8-) i want to take information from like a rss thing...i want a dictionary...so how would i take information from like a site without writing a dictionary myself?


RE: Need VB help(Visual Basic) by CookieRevised on 10-22-2005 at 02:05 PM

Sorry, but as you can read in this thread yourself you weren't able to handle easy stuff properly (or not at all). You simply copy/pasted the examples given to you which were meant to be studied and above all to be adapted to your own program instead.

So, in order to explain how to do such things as you ask, we need to write a book, as you don't have any basis.


quote:
Originally posted by Ezra
start with the easy stuff, learn why things behave like they do, and work onwards from that.
(...)
Just take it one step at a time, and you'll learn much quicker and better

RE: Need VB help(Visual Basic) by ShawnZ on 10-22-2005 at 02:53 PM

God sake dylan, if you can't do it, don't try.

Maybe you should learn the basics before you try, fail, give up, and come here for answers.