if statement - VBA 2012 - Need to return an error when a user enters text instead of a numbers -
i taking vba class , stuck on problem. cannot use masked text box, solve problem. instead professor wants me learn code, can imagine?
all kidding aside, user needs enter gas price text box, hit calculate receive total cost of trip. there more interface spare details. if user enters else number positive number 1 decimal place, should return error. have figured out 0 or 0000 negative number such -3.45. have text or special characters give me error 34.56.12.45. never know, user may feel need type in ip address. key assignment catch probable user errors.
here i've written calculation catch errors. have tried try/catch statements well. nothing worked got first 2 parts of if statement work yet failing on last if part until gets calculation.
private sub btncalc_click(sender object, e eventargs) handles btncalc.click
dim mileage decimal dim miles decimal dim gasprice decimal dim cost decimal if cdec(txtbxgasprice.text) = 0 messagebox.show("please enter positive dollar amount") txtbxgasprice.text = string.empty end if if cdec(txtbxgasprice.text) < 0 messagebox.show("please enter positive dollar amount") txtbxgasprice.text = string.empty end if if cost = cdec((miles / mileage) * gasprice) miles = cdec(lbltmiles.text) mileage = cdec(lblmileage.text) gasprice = cdec(txtbxgasprice.text) lbltotalcost.text = cost.tostring("c2") end if if cbool(txtbxgasprice.text = "") msgbox("you must enter dollar amount") end if *if not isnumeric(txtbxgasprice.text) messagebox.show("please enter positive dollar amount") txtbxgasprice.text = string.empty* end if end sub
'i have placed @ top, in middle, @ bottom no luck. missing?
appreciate thoughts - lauren
this 1 seems meet criteria , pass david's tests:
function isvalid(txt string) boolean if not isnumeric(txt) exit function end if if len(txt) < 2 exit function end if if not mid(txt, len(txt) - 1, 1) = "." exit function end if if not txt > 0 exit function end if isvalid = true end function
Comments
Post a Comment