Jump to content

vb help


jrwcmj

Recommended Posts

whats wrong with my code here it is

  

Dim sngLength As Single
Dim sngWidth As Single
Dim sngArea As Single
Dim sngPrice As Single
Dim sngUnderlay As Single
Dim sngFitting As Single
Dim sngTotal As Single




Private Sub cmdCalculate_Click()

sngLength = txtLength
sngWidth = txtWidth
sngArea = sngLength * sngWidth

If cboTypeOfUnderlay = "Economy" Then
    sngUnderlay = sngArea * 1.75
Else
If cboTypeOfUnderlay = "Luxury" Then
    sngUnderlay = sngArea * 2.15
End If
sngPrice = txtPricePerSqMetre
sngFitting = sngArea * 2.5
sngTotal = (sngArea * sngPrice) + Fitting + Underlay

'Use the FORMAT function to change the final value into currency
txtTotalCosts = Format(sngTotal, "currency")
End If
End Sub

Private Sub Form_Load()
cboTypeOfUnderlay.AddItem "Economy"
cboTypeOfUnderlay.AddItem "Luxury"


End Sub

Link to comment
Share on other sites

lol i kind of write it i had to fix a few fings but i think there might be a few things still wrong with the code

Let me see....

the  ecomy does not work

Yep I'd say there might be a few things still wrong with the code. Would you like to enlighten us as to what you are on about ?

I figure I'll be nice anyway, your problem is in here...

If cboTypeOfUnderlay = "Economy" Then
    sngUnderlay = sngArea * 1.75
Else
If cboTypeOfUnderlay = "Luxury" Then
    sngUnderlay = sngArea * 2.15
End If
sngPrice = txtPricePerSqMetre
sngFitting = sngArea * 2.5
sngTotal = (sngArea * sngPrice) + Fitting + Underlay

'Use the FORMAT function to change the final value into currency
txtTotalCosts = Format(sngTotal, "currency")
End If

It should look like this

If cboTypeOfUnderlay = "Economy" Then
    sngUnderlay = sngArea * 1.75
ElseIf cboTypeOfUnderlay = "Luxury" Then
    sngUnderlay = sngArea * 2.15
End If
sngPrice = txtPricePerSqMetre
sngFitting = sngArea * 2.5
sngTotal = (sngArea * sngPrice) + Fitting + Underlay

'Use the FORMAT function to change the final value into currency
txtTotalCosts = Format(sngTotal, "currency")

Notice the change from

Else
If cboTypeOfUnderlay = "Luxury" Then

To...

ElseIf cboTypeOfUnderlay = "Luxury" Then

I'm gonna assume your learning VB in school, I find most teachers actually don't know VB apart from the basics, If you need help remember Google is your friend and If you post here (coherently) you will get help.

Link to comment
Share on other sites

vb did not like that for some reason i am useing vb6 by the way

How did VB not like that? What was the error?

When asking for help you have to remember we only know what you tell us, so the more information you give the easier it is for us to help you.

Link to comment
Share on other sites

I'm actually a bit puzzled why going from

IF A

  blah1

ELSE

  IF B

    blah2

  END IF

  blah3

END IF

to

IF A

  blah1

ELSEIF B

  blah2

END IF

blah3

can be considered equal, yet fixing of a problem. I see what might be an optimization, and how one might think that blah3 is critical to the functioning of this method, but it could very well be that the statements in blah3 must not be executed when A was true. You'd need context.

And yes, simply stating "the  ecomy does not work" doesn't make sense, and most certainly isn't the actual error message he was getting. Try again.

Link to comment
Share on other sites

IF A          < If the first condition is true then...

  blah1        < Do stuff

ELSE        < If the first wasn't true, do this...

  IF B            < Check the whole thing again

    blah2          < Do this if B was true

  END IF        < Finish the stuff that happened if B was true

  blah3          < Do more stuff if condition A was false, ignore the check you just did with B

END IF      < Finish

If A is true run blah1, ignore B

If A is false and B is false run blah3

If A is false and B is true run blah2 and blah3

IF A          < If the first condition is true then...

  blah1        < Do stuff

ELSE IF B  < If A was false, check B

    blah2          < Do this if B was true

END IF      < If neither were true then finish

blah3        < Do this after all the checks

If A is true run blah1 and blah3, ignore B

If A is false and B is false run blah3

If A is false and B is true run blah2 and blah3

So in short, the difference is that in the first snippet blah3 (in this case the calculation and output) ONLY gets executed if A is false, in snippet 2 it gets executed whatever happens.

The calculation and output would only happen in the second snippet because in the first it gets ignored if you chose "Economy".

Link to comment
Share on other sites

Ah, that's the "doesn't work" bit relating to "ecomy".

I noticed the difference in semantics, just didn't understand why doing it the other way would be more right since you didn't get to see any of the actual calling code. For all I knew that output wasn't _supposed_ to be generated when Economy was selected.

Link to comment
Share on other sites

One thing I did notice is that you're not using properties correctly, each control has many properties and to ensure your program works and the code is readable you must use them.

For example:

txtTotalCosts = Format(sngTotal, "currency")

Should be:

txtTotalCosts.Text = Format(sngTotal, "currency")

You'll need to do the same with the combo boxes too (of course using the relevant property).

Link to comment
Share on other sites

thanks here's what the code looks like now:

Private Sub cmdCalculate_Click()

Dim sngLength As Single

Dim sngWidth As Single

Dim sngArea As Single

Dim sngPrice As Single

Dim sngUnderlay As Single

Dim sngFitting As Single

Dim sngTotal As Single

sngLength = txtLength

sngWidth = txtWidth

sngArea = sngLength * sngWidth

If cboTypeOfUnderlay = "Economy" Then

    sngUnderlay = sngArea * 1.75

ElseIf cboTypeOfUnderlay = "Luxury" Then

    sngUnderlay = sngArea * 2.15

End If

sngPrice = txtPricePerSqMetre

sngFitting = sngArea * 2.5

sngTotal = (sngArea * sngPrice) + sngFitting + sngUnderlay

'Use the FORMAT function to change the final value into currency

txtTotalCosts.Text = Format(sngTotal, "currency")

End Sub

Private Sub Form_Load()

cboTypeOfUnderlay.AddItem "Economy"

cboTypeOfUnderlay.AddItem "Luxury"

i what to add an exit button but i don't know how to do it can you help me

Link to comment
Share on other sites

I get the feeling you haven't really done any VB before...

Private Sub cmdCalculate_Click()

Dim sngLength As Single

Dim sngWidth As Single

Dim sngArea As Single

Dim sngPrice As Single

Dim sngUnderlay As Single

Dim sngFitting As Single

Dim sngTotal As Single

sngLength = txtLength                                                                              ' Needs a property.

sngWidth = txtWidth                                                                                  ' Needs a property.

sngArea = sngLength * sngWidth

If cboTypeOfUnderlay = "Economy" Then                                                  ' Needs a property.

    sngUnderlay = sngArea * 1.75

ElseIf cboTypeOfUnderlay = "Luxury" Then                                              ' Needs a property

    sngUnderlay = sngArea * 2.15

End If

sngPrice = txtPricePerSqMetre                                                                  ' Needs a property

sngFitting = sngArea * 2.5

sngTotal = (sngArea * sngPrice) + sngFitting + sngUnderlay

'Use the FORMAT function to change the final value into currency

txtTotalCosts.Text = Format(sngTotal, "currency")

End Sub

Private Sub Form_Load()

cboTypeOfUnderlay.AddItem "Economy"

cboTypeOfUnderlay.AddItem "Luxury"

i what to add an exit button but i don't know how to do it can you help me

To add an exit button, add a command button control, call it cmdExit (or whatever you want it to be called), change the caption, then add:

Sub cmdExit_Click()

      End

End Sub

Link to comment
Share on other sites

All these errors look like... well... dumb mistakes that nobody with any experience in the language should be making. If this really is your teacher's code, you should wonder aloud just what it is you think you can still learn from him.

Link to comment
Share on other sites

All these errors look like... well... dumb mistakes that nobody with any experience in the language should be making. If this really is your teacher's code, you should wonder aloud just what it is you think you can still learn from him.

I was guessing it was a debugging exercise where the teacher gives you a basic but broken program and the idea is you have to fix it. Seems to be a great way to learn once you've got the basics down, I stated teaching someone the same way a little while ago.

Link to comment
Share on other sites

Try this:

Dim sngLength As Single
Dim sngWidth As Single
Dim sngArea As Single
Dim sngPrice As Single
Dim sngUnderlay As Single
Dim sngFitting As Single
Dim sngTotal As Single




Private Sub cmdCalculate_Click()

sngLength = txtLength
sngWidth = txtWidth
sngArea = sngLength * sngWidth

'assuming cboTypeOfUnderlay is a combo box/drop down combo
If cboTypeOfUnderlay.Text = "Economy" Then sngUnderlay = sngArea * 1.75
If cboTypeOfUnderlay.Text = "Luxury" Then sngUnderlay = sngArea * 2.15

sngPrice = txtPricePerSqMetre
sngFitting = sngArea * 2.5
sngTotal = (sngArea * sngPrice) + Fitting + Underlay

'Use the FORMAT function to change the final value into currency
txtTotalCosts = Format(sngTotal, "currency")

End Sub

Private Sub Form_Load()
cboTypeOfUnderlay.AddItem "Economy"
cboTypeOfUnderlay.AddItem "Luxury"


End Sub

WHat I don't get is this line though: sngTotal = (sngArea * sngPrice) + Fitting + Underlay

shouldnt it be: sngTotal = (sngArea * sngPrice) + sngFitting + sngUnderlay

Link to comment
Share on other sites

Ok. I got it working. Copy and paste this in notepad and then save it as jrwcmj1.frm then run the form (should work by just double clicking it) or add it to a project.

VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "jrwcmj vb program"
   ClientHeight    =   4335
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   3870
   LinkTopic       =   "Form1"
   ScaleHeight     =   4335
   ScaleWidth      =   3870
   StartUpPosition =   3  'Windows Default
   Begin VB.TextBox txtTotalCosts 
      Height          =   285
      Left            =   360
      Locked          =   -1  'True
      TabIndex        =   8
      Text            =   "txtTotalCosts"
      Top             =   3600
      Width           =   2895
   End
   Begin VB.ComboBox cboTypeOfUnderlay 
      Height          =   315
      Left            =   240
      TabIndex        =   7
      Top             =   120
      Width           =   1815
   End
   Begin VB.TextBox txtWidth 
      Height          =   285
      Left            =   360
      TabIndex        =   6
      Text            =   "0"
      Top             =   1200
      Width           =   1695
   End
   Begin VB.TextBox sngTotaltxt 
      Height          =   285
      Left            =   360
      Locked          =   -1  'True
      TabIndex        =   5
      Text            =   "sngTotal"
      Top             =   3240
      Width           =   2895
   End
   Begin VB.TextBox txtPricePerSqMetre 
      Height          =   285
      Left            =   360
      TabIndex        =   4
      Text            =   "0"
      Top             =   1560
      Width           =   1695
   End
   Begin VB.TextBox txtUnderlay 
      Height          =   285
      Left            =   360
      Locked          =   -1  'True
      TabIndex        =   3
      Text            =   "sngUnderlay"
      Top             =   2880
      Width           =   2895
   End
   Begin VB.TextBox text1 
      Height          =   285
      Left            =   360
      Locked          =   -1  'True
      TabIndex        =   2
      Text            =   "sngArea"
      Top             =   2520
      Width           =   2895
   End
   Begin VB.TextBox txtLength 
      Height          =   285
      Left            =   360
      TabIndex        =   1
      Text            =   "0"
      Top             =   840
      Width           =   1695
   End
   Begin VB.CommandButton cmdCalculate 
      Caption         =   "Calculate"
      Height          =   375
      Left            =   2400
      TabIndex        =   0
      Top             =   120
      Width           =   1215
   End
   Begin VB.Frame Frame1 
      Caption         =   "Enter Variable Options"
      Height          =   1575
      Left            =   120
      TabIndex        =   9
      Top             =   480
      Width           =   3615
      Begin VB.Label Label3 
         Caption         =   "txtPricePerSqMetre"
         Height          =   255
         Left            =   2040
         TabIndex        =   12
         Top             =   960
         Width           =   1455
      End
      Begin VB.Label Label2 
         Caption         =   "txt Width"
         Height          =   255
         Left            =   2040
         TabIndex        =   11
         Top             =   600
         Width           =   855
      End
      Begin VB.Label Label1 
         Caption         =   "txt Length"
         Height          =   255
         Left            =   2040
         TabIndex        =   10
         Top             =   240
         Width           =   855
      End
   End
   Begin VB.Frame Frame2 
      Caption         =   "Fixed Text Output"
      Height          =   1935
      Left            =   120
      TabIndex        =   13
      Top             =   2160
      Width           =   3615
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim sngLength As Single
Dim sngWidth As Single
Dim sngArea As Single
Dim sngPrice As Single
Dim sngUnderlay As Single
Dim sngFitting As Single
Dim sngTotal As Single


Private Sub cmdCalculate_Click()


'assuming cboTypeOfUnderlay is a combo box/drop down combo
If cboTypeOfUnderlay.Text = "" Then GoTo exitsub  'Do not process anything until they make a choice
If cboTypeOfUnderlay.Text = "Economy" Then sngUnderlay = sngArea * 1.75
If cboTypeOfUnderlay.Text = "Luxury" Then sngUnderlay = sngArea * 2.15

sngLength = txtLength
sngWidth = txtWidth
sngArea = sngLength * sngWidth
text1.Text = sngArea

txtUnderlay = sngUnderlay
sngPrice = txtPricePerSqMetre
sngFitting = sngArea * 2.5
sngTotal = (sngArea * sngPrice) + sngFitting + sngUnderlay
sngTotaltxt = sngTotal
'Use the FORMAT function to change the final value into currency
txtTotalCosts = Format(sngTotal, "currency")
Exit Sub
exitsub:
    MsgBox "Please select Economy or Luxury from the Drop Down Box!"
    
End Sub

Private Sub Form_Load()
cboTypeOfUnderlay.AddItem "Economy"
cboTypeOfUnderlay.AddItem "Luxury"


End Sub

I added a few things but this works prefectly so long as they enter whole numbers in the variable text boxes. If they add text(letters not numbers or any other character) the program will abend. You can add things to force it to only allow numbers or decimals, etc, but that is up to you.

If you have these element I used already on the form(which I cannot tell since you only gave us the source of the form and not the GUI itself) you can just use the frm source:

Dim sngLength As Single
Dim sngWidth As Single
Dim sngArea As Single
Dim sngPrice As Single
Dim sngUnderlay As Single
Dim sngFitting As Single
Dim sngTotal As Single


Private Sub cmdCalculate_Click()


'assuming cboTypeOfUnderlay is a combo box/drop down combo
If cboTypeOfUnderlay.Text = "" Then GoTo exitsub  'Do not process anything until they make a choice
If cboTypeOfUnderlay.Text = "Economy" Then sngUnderlay = sngArea * 1.75
If cboTypeOfUnderlay.Text = "Luxury" Then sngUnderlay = sngArea * 2.15

sngLength = txtLength
sngWidth = txtWidth
sngArea = sngLength * sngWidth
text1.Text = sngArea

txtUnderlay = sngUnderlay
sngPrice = txtPricePerSqMetre
sngFitting = sngArea * 2.5
sngTotal = (sngArea * sngPrice) + sngFitting + sngUnderlay
sngTotaltxt = sngTotal
'Use the FORMAT function to change the final value into currency
txtTotalCosts = Format(sngTotal, "currency")
Exit Sub
exitsub:
    MsgBox "Please select Economy or Luxury from the Drop Down Box!"
    
End Sub

Private Sub Form_Load()
cboTypeOfUnderlay.AddItem "Economy"
cboTypeOfUnderlay.AddItem "Luxury"


End Sub

Link to comment
Share on other sites

thank can you look at this code for me

Public iTotalpoints As Integer

Public iLivesleft As Integer

Private Sub endofgame()

Lbltotalpoints.FontSize = 24

Lbltotalpoints.ForeColor = vbRed

Lbltotalpoints.BackColor = vbYellow

Lbltotalpoints.FontBold = True

Lbltotalpoints.Caption = "congratulations you scored:" & iTotalpoints & " points "

If iLivesleft >= 0 Then

Lbllivesleft.Caption " lives left = " & iLivesleft

Else

Exit Sub

End If

End Sub

Private Sub cmdEndClick()

' end the game by telling telling them what they could have won.

Call endofgame

End

End Sub

Private Sub Cmdgo_Click()

Dim iSkillSpeed As Integer

If iLivesleft = 0 Then

MsgBox "game over", vbInformation, "targets game "

Cmdreset.Visible = True

Else

'pass the function to control the speed of the target

iSkillSpeed = 5

iskillnumber = iSkillSpeed

skilllevel (iskillnumber)

cmd.visable = False

iLivesleft = iLivesleft - 1

Lbllivesleft.Caption = "livesleft = " & iLivesleft

If iTotalpoints <= 10 Then

'end game

cmdend.Visible = True

Call endofgame

End If

Image1.Left = 11040

cmdend.Visible = True

End If

Lbltotalpoints.Caption = " total points = " & iTotalpoints

End Sub

Private Sub Cmdreset_Click()

Call reset_game

End Sub

Private Sub reset_game()

imageleft = 11040

cmdend = True

iTotalpoints = 0

iLivesleft = 3

Lbllivesleft.Caption = "lives left = " & iLivesleft

Lbltotalpoints.Caption = "total points = " & iTotalpoints

End Sub

Private Sub Form_Load()

Call reset_game

Frmtargets.BackColor = vbBlack

Lbltotalpoints.FontSize = 24

Lbltotalpoints.ForeColor = vbBlue

Lbltotalpoints.BackColor = vbWhite

Lbltotalpoints.Alignment = vbCenter

Lbltotalpoints.FontBold = True

Lbllivesleft.FontSize = 24

Lbllivesleft.ForeColor = vbBlue

Lbllivesleft.BackColor = vbWhite

Lbllivesleft.Alignment = vbCenter

Lbllivesleft.FontBold = True

cmdend.Visible = False

Cmdreset.Visible = True

End Sub

Private Sub Image1_mousedown(button As Integer, shift As Integer, x As Single, y As Single)

iTotalpoints = iTotalpoints + 1

MsgBox "you scrored another point, your total points are: & itotalpoints"

Lbltotalpoints.Caption = "total point = " & iTotalpoints

End Sub

Private Function skilllevel(iskillnumber)

Dim iSkillSpeed As Single

If Option1 = True Then

iSkillSpeed = 1

ElseIf Option2 = True Then

iSkillSpeed = 1.3

ElseIf Option3 = True Then

iSkillSpeed = 5

End If

Do

For x = 1 To iskillnumber '10000

Image1.Left = Image1.Left - skillspeed

DoEvents

Next x

Loop Until Image1.Left < 10

End Function

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...