BASIC/Visual BASIC Thread

FXB

New Member
The second error tells you what you need to do ;)
Code:
You have:
frmCafeJean.Hide()
You want:
Me.Hide()

but then how do specify which form I want to show or hide?
 

S.T.A.R.S.

banned
but then how do specify which form I want to show or hide?

You can show or hide any forms by doing that from any form you want.However you will need to call all those forms and give them different names above the "public Form1" or which ever form you want to use for that.This is the C# example:

Form1 frm1=new Form1();
Form2 frm2=new Form2();
Form3 frm3=new Form3();
public Form1()
{
//Here is the entire code of your project of course...
}

And now if for example from the Form1 you want to Show,Hide,Close,Dispose or do anything else with any form like Form2 and Form3,just call them and write the property.For example you want to show Form2,but close Form3.To do that,simply write the following code in the event you are using for that:

frm2.Show();
frm3.Close();

NOTE: Once the Form3 is closed,you will NOT be able to open it again using the "frm3.Show();".That is because after the Form3 was closed,it tells to the compiler that it is actually DISPOSED so if you try to open the Form3 again after you closed it,you will get the runtime exception error that says something like CANNOT ACCESS THE DISPOSED OBJECT.That is normal because your application AUTOMATICALLY disposed the Form3 even though you just used the Close(); method.That is kinda cool because at least you do not need to write the code to dispose that too in order to remove it from the memory and make program run faster and better.Pretty cool ha =) ?
Well anyway to again open the "disposed" Form3,just use the following code:

if(Form3.IsDisposed==true)
{
frm3=new Form3();
frm3.Show();
}
if(Form3.IsDisposed==false) //Or simply use the "else" keyword here...
{
frm3.Show();
}


So this was the C# example,but the way is the same in VB.It's even easier.Just be sure that the "Me" is the keyword for the current form and "this" in the C#.Also its very easy to show any form in VB for the difference of C#.Just write:

Form3.Show()

That will show/open Form3 immediately.To close it,way is the same:

Form3.Close()


I personally do not use VB so try this and if you get an error,let me know what it says.
 

Cromewell

Administrator
Staff member
Please don't post C# in this thread, I know it's all part of the .NET family but this isn't the place for it. It may confuse someone who is writing in VB.

but then how do specify which form I want to show or hide?

You use Me to refer to the current form your code is in. You can still refer to the other forms by their name. So:
Code:
Me.Hide()
frmDejeuner.Hide()
Because the code that is executing is in the frmCafeJean form, Me refers to frmCafeJean. In your code for frmDejeuner, Me would refer to frmDejeuner.
 

WeatherMan

Active Member
Okay I'm a bit stuck



I've got 3 buttons (select your quiz), a label (selection clicked) and a submit button

Here is the code I have at the moment

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If strGenre = "Music" Then Music_Quiz.Show()
If strGenre = "Computer Games" Then Computer_Games_Quiz.Show()
If strGenre = "Movies" Then Movies_Quiz.Show()
If strGenre = "" Then MsgBox("Please select a quiz!")
End Sub

As you can see I'd like a message box to appear if nothing has been selected. I was wondering
how I could get this to work with an Else tag

I've tried

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If strGenre = "Music" Then Music_Quiz.Show()
If strGenre = "Computer Games" Then Computer_Games_Quiz.Show()
If strGenre = "Movies" Then Movies_Quiz.Show() Else
MsgBox ("Please select a quiz!")
End Sub

and

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If strGenre = "Music" Then
Music_Quiz.Show()
End If
If strGenre = "Computer Games" Then
Computer_Games_Quiz.Show()
End If
If strGenre = "Movies" Then Movies_Quiz.Show()
Else
MsgBox ("Please select a quiz!")
End Sub

But because 'Else' only covers one submit at a time, my other 2 items (Computer Games)
& (Movies) flag up as Else aswell.

Is there any way to get Music, Computer Games & Movies to bring up their specific forms and use Else to filter out anything other than them 3 words + bring up a message box?

Surely I'm doing something wrong, I just can't work out what :]

I've tried adding Else tags each If which didn't work.
If I could do something like If strGenre = "Music" Then Music_Quiz.Show / *strGenre = "Computer Games" Then Computer_Games.Show / strGenre = "Movies" Then Movies_Quiz.Show Else
MsgBox ("Please select a quiz!")

That would do me fine, But sadly it doesn't work like that :p

Any advice?

Thanks
Jason
 

Cromewell

Administrator
Staff member
You have a couple options: If then elseif then else or use a select case.

If strGenre = "Music" Then Music_Quiz.Show()
elseIf strGenre = "Computer Games" Then Computer_Games_Quiz.Show()
elseIf strGenre = "Movies" Then Movies_Quiz.Show() Else
MsgBox ("Please select a quiz!")

or

select case strGenre
case "Music"
Music_Quiz.show()
case "Computer Games"
Computer_Games_Quiz.Show()
case "Movies"
Movies_Quiz.Show()
case Else
MsgBox ("Please select a quiz!")
end select
 

WeatherMan

Active Member
Thanks for that! :)

The second option seems to work flawlessly :]

I've now purchased a Visual Basic book to let me learn the language a little better, have another software design class tomorrow morning.

Will be asking my lecturer about this tomorrow but I was wondering If you could help me with a new thing I'm stuck with.

I've changed strGenre from a label to a textbox and it's working with the If & Else tags.

I can click the buttons and it'll bring up my forms, I can clear the textbox and it'll bring up my MsgBox, I can write random letters and it'll also bring up my MsgBox.

The problem is, I've specified when 'StrGenre' = 'Music' (or any other of my buttons), it'll open if I've pressed the button, but not if I type the value in.

Any idea's what's up? :)

Edit - Sorted!

All I needed to do was change the variable from "strGenre" to TextBox2.Text :] All working now! Thanks for your help.
 
Last edited:

bratsos

New Member
Hello world !!!

Can anyone show me some good tutorials in v.b links with live interactivity.
Eg: To go next stage of tutorial, you must pass first this task of current tutorial.
 

jllipke

Member
What exactly are you looking for? I won't do your homework but I don't mind helping you with certain aspects of it.

In this case, it's a relatively simple project so I'd suggest planning out/designing the initial UI that will be presented then slowly building up the backing code and any other UI objects you need to make it work.

I'm not big on VB2008, the last version I used was 6.
Visual Basic 6 is best
 

bratsos

New Member
Code:
You have:
LblSaisieNom = txtSaisieNom.Text
You want:
LblSaisieNom.Text = txtSaisieNom.Text
The second error tells you what you need to do ;)
Code:
You have:
frmCafeJean.Hide()
You want:
Me.Hide()


True.
And a small mistake in programming tree, cause many issues.
At least is no logical mistake:D
Me at programming i make many logical mistakes :eek:
 

S.T.A.R.S.

banned
True.
And a small mistake in programming tree, cause many issues.

Oh yea.I just had a huge bug in my UAC project yesterday which contains TONS of complicated lines and on which I work for about 4 months already.More than 15000 lines in one form.And I had a strange bug which I had no idea why it was happening.At the end after reading ALL the lines with a huge toture (lol) I found out I used wrong string value LOOOOOOOOOOOOOL.It was so damn simple and I got so pissed off for losing so much time on that crappy simple bug lol,but then agian like you say small and simple things can cause many issues.:D
I really hate programming sometimes :D
 

CrazyMike

New Member
Alright guys / gals,

I have recently found myself in a programming predicament. I have never programmed (write code) in any way shape or form before. So this is all new to me. I have been Googling and YouTube-ing as much as i can, and find myself stuck right now.

Here's what's going on:

So i have created a "Main page" in excel with buttons on it. Each button has it's purpose.

The buttons on the main page:




So when the first three buttons are pushed, a pop up window comes up (something like the following picture:



I cannot figure out what the code would be for the "Submit" button. I need it so when variable value is inputed (in this case serial number) it will open a specific workbook and match the inputed number with the worksheet name in that workbook. Would also like an error to pop up if part number is not found and allow user to retry. As well as close window after done search.

This next one, i cannot figure out the "Submit" button again. I need the code to input all data into a worksheet in the proper table columns.



If you notice there is one that is blue, I would like it if when part number is inputed, the description automatically pops up. As well with error code on this one if a field is left blank.

This last one uses the same data as previous and searches this said table.




Just type in any of the ONE field, and the search will pop up anything corresponding to that specific search.


So basically i am looking for any help in creating these codes. Either help/link to better understanding VB in how code is written (specific to my situation) or help writing in general.

Thanks for your support.
 

Cromewell

Administrator
Staff member
I cannot figure out what the code would be for the "Submit" button. I need it so when variable value is inputed (in this case serial number) it will open a specific workbook and match the inputed number with the worksheet name in that workbook. Would also like an error to pop up if part number is not found and allow user to retry. As well as close window after done search.
This isn't how to do it, but just an example I found online of how to search through a worksheet for a specific value. You should be able to modify it to work in your project.
Code:
Sub Find_First()
    Dim FindString As String
    Dim Rng As Range
    FindString = InputBox("Enter a Search value")
    If Trim(FindString) <> "" Then
        With Sheets("Sheet1").Range("A:A")
            Set Rng = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                Application.Goto Rng, True
            Else
                MsgBox "Nothing found"
            End If
        End With
    End If
End Sub
To write a value to a cell you can use
Code:
Worksheets("Sheet1").Cells(x, y) = value
where x is the row and y is the column (both are numbers).
 

S.T.A.R.S.

banned
I have never programmed (write code) in any way shape or form before. So this is all new to me......

If you have never written any programming code before then FIRST forget about thinking to make this to work great in 1 day.
What you need is to start learning programming from the beginning.And when I say BEGINNING,I mean on something like HELLO WORLD application.

You cannot create any good applications/programs if you have never ever written any programming code before.You need to start from the beginning and first learn basic things and write the most simple programming line such as:

MessageBox.Show("Hello World")

Then understand that line and slowly move on and learn more and more.You can either use books,tutorials or other internet resources.

Once you have that knowledge THEN you can create programs to do things you want.You cannot just simply find the code for let's say SUBMIT button and then perform copy/paste and make it work lol.Because if you do,it is NOT going to work properly because that code you copied and pasted could be written for God knows what.And I assure you that the code you will copy and paste will NOT do exactly what you want...it will do something else what the programmer who originally wrote it for ;)

You need to understand the programming logic well and once you do,you can do whatever you want.So start from the beginning and not from the middle like you do right now.You cannot expect to make that in one day.You first need to learn a lot of things before that.Programming is not so easy as it may seem.
Take a look at the following code example:
pc_net_vb_code.jpg


You probably don't understand it right? That is why you must learn it well and understand the logic before you are able to create something on your own.If you do simple copy and paste from the above image and paste it into your program,it will NOT work and reasons can be MANY such as:

-the specified control name cannot be found...
-assembly reference...
-method that is being used cannot be found...
-DLLs (if any)...
-events that are not created...
-string and other values you use were not declared...
-and MUCH more...

So you cannot just take the code and use it immediately.It will simply not work if you do not know and understand what you are doing.First start from something simple like HELLO WORLD programs and then move on step by step.;)





Cheers!
 

CrazyMike

New Member
Thank you guys so much. I do understand somewhat of the code. Like the example you have given S.T.A.R.S, i understand portions of the code "Hello World". Nowhere near understanding all of it.

The code that Cromewell has provided helps me out so much. I was mainly confused of the starting of the code, and the example he has given me helps me out on where to start. Thanks.

I'm actually in the market of a book to further understand VBA. It's going to take me time but i think ill get it figured out.

If either of you know a good book or online training that could help me learn the language of VBA, please let me know :). Much like the example S.T.A.R.S has given with the "hello world", but goes further in depth.

Thanks again!!
 

S.T.A.R.S.

banned
This is exactly what you need:

http://www.amazon.com/Microsoft-Pro...9005/ref=sr_1_1?ie=UTF8&qid=1327164562&sr=8-1


All the things you learn from that book can also be applied for newer VB 2010.It can of course also be applied in older VB such as 2002 and 2003 and 2005,but in VB 2002 and probably 2003 it might require some extra code to be written in order for examples from that book to work.But don't use VB 2002 and 2003.They are old.Feel free to use VB 2010 for this book.



Cheers!
 
Last edited:

CrazyMike

New Member
This is exactly what you need:

http://www.amazon.com/Microsoft-Pro...9005/ref=sr_1_1?ie=UTF8&qid=1327164562&sr=8-1


All the things you learn from that book can also be applied for newer VB 2010.It can of course also be applied in older VB such as 2002 and 2003 and 2005,but in VB 2002 and probably 2003 it might require some extra code to be written in order for examples from that book to work.But don't use VB 2002 and 2003.They are old.Feel free to use VB 2010 for this book.



Cheers!

It says 2008? Anyway, if you say it will help for the 2010 version, I'm on my way to the store to pick it up :) Thanks for the suggestion.
 

CrazyMike

New Member
Hey guys/gals

I am learning how to code in VBA while doing up a project. I went to pick up a few books and reading while practicing. Basically i just need to help to figure out whats going on.

Scenario:

I have a form that i am inputting info into a separate workbook/worksheet. So the user clicks on a button, form pops up, they input all the information, and the information clears after submitting (with all info inputted into the workbook/worksheet specified).

The following is the code i came up with:

Code:
Private Sub cmdcancel_Click()
Unload frmcompletedsealinput
End Sub

Private Sub cmdsubmit_Click()
    Dim iRow As Long
    Dim ws As Worksheets
    Dim wb As Workbook
    Set wb = Workbooks("")
    Set ws = Worksheets("Sheet1")

    'Find Last Row in table
    iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
    
    'check for a Job Number
    If Trim(Me.txtjobnumber.Value) = "" Then
    Me.txtjobnumber.SetFocus
    MsgBox "Please enter a job number"
    End If

    'check for a Serial Number
    If Trim(Me.txtserialnumber.Value) = "" Then
    Me.txtserialnumber.SetFocus
    MsgBox "Please enter a Serial Number"
    End If

    'check for a Date
    If Trim(Me.txtdate.Value) = "" Then
    Me.txtdate.SetFocus
    MsgBox "Please enter a Date"
    End If
    
    'check for a Seal Part Number
    If Trim(Me.txtsealpartnumber.Value) = "" Then
    Me.txtsealpartnumber.SetFocus
    MsgBox "Please enter a Seal Part Number"
    End If
    
    'check for a Customer Name
    If Trim(Me.txtcustomername.Value) = "" Then
    Me.txtcustomername.SetFocus
    MsgBox "Please enter a Customer Name"
    End If
    
    'check for a Location
    If Trim(Me.txtlocation.Value) = "" Then
    Me.txtlocation.SetFocus
    MsgBox "Please enter a Location"
    End If
    
    'Copy data to the Completed seal table
    wb.ws.Cells(iRow, 2).Value = Me.txtsealpartnumber.Value
    wb.ws.Cells(iRow, 3).Value = Me.txtserialnumber.Value
    wb.ws.Cells(iRow, 4).Value = Me.txtcustomername.Value
    wb.ws.Cells(iRow, 6).Value = Me.txtjobnumber.Value
    wb.ws.Cells(iRow, 7).Value = Me.txtdate.Value
    wb.ws.Cells(iRow, 9).Value = Me.txtlocation.Value
    
             'Reset data for another entry
            Me.txtsealpartnumber.Value = ""
            Me.txtserialnumber.Value = ""
            Me.txtcustomername.Value = ""
            Me.txtjobnumber.Value = ""
            Me.txtdate.Value = ""
            Me.txtlocation.Value = ""
    
End Sub

In this code there are several things; wb values, ws value. a formula/code to find the last availiable line in table, input codes, empty slot error codes and clear codes.

When i try out this code, the form pops up, i input data, click the submit button, but nothing pops up. Just goes back to my code and highlights "
Private Sub cmdsubmit_Click()". No data was entered in the workbook/worksheet.

just wanted to know if anyone can take a look at this and see where i could possible going wrong or doing wrong (*besides everything lol*).

Thanks
 

Cromewell

Administrator
Staff member
If it's jumping to code there's usually a runtime error. I'm guessing this line: Set wb = Workbooks("") is the problem.

What version of excel are you using?
 
Top