120 likes | 217 Views
Miscellaneous Visual Basic Questions and Answers. Screen/Button Manipulations. Unload Form Load Form Form.Show Form.Hide ControlButton.Visible = True/False cmdNextPage.Visible = True (shows the button) cmdNextPage.Visible = False (hides the button). Subroutines/Functions.
E N D
Screen/Button Manipulations • Unload Form • Load Form • Form.Show • Form.Hide • ControlButton.Visible = True/False • cmdNextPage.Visible = True (shows the button) • cmdNextPage.Visible = False (hides the button)
Subroutines/Functions • Access to variables • Form variables • Global variables • arguments passed to/from the function • Calling statement • Call functionname (arguments) • Function statement • Private Function functionname (arguments)
Autonumber in ACCESS • Order number in ACCESS can be defined as autonumber • Autonumber is a “long integer” • Access will automatically increment the order number by one for each new order • Visual Basic is not so nice!
How to do a query on an integer field Dim strFind as string Dim num as Integer num = val(txtOrder.text) strFind = “[Order Number] = “ & num datOrder.Recordset.FindFirst strFind If datOrder.Recordset.Nomatch then msgbox (“order number: “ & txtOrder.text & “ not on file”) End If
Date Fields and Functions • lblDate.caption = Now • Now: mm-dd-yy hh:mm:ss • What if you only want part of this field • orddate = Now • lblMonth.caption = Month(orddate) • lblDay.caption = Day(orddate) • lblYear.caption = Year(orddate) • lblDate.caption = Month(orddate) & “-” & Day(orddate) & “-” Year(orddate)
Another Date Method • Create a field in Access: order date • Type: Date/Time • Subtype: Short Date (mm-dd-yy) • Default: Now() • ACCESS will automatically short today’s date in the mm-dd-yy format on any records added to the ACCESS database by Visual Basic
Advantages/Disadvantages • Allowing ACCESS to insert today’s date automatically • Using functions in Visual Basic to update the fields with the date
How to add the next order record Dim newnum as integer Dim orddate as date datOrder.Recordset.MoveLast newnum = datOrder.Recordset.Fields (“Order Number”).value + 1 datOrder.Recordset.Addnew datOrder.Recordset.Fields (“Order Number”) = newnum datOrder.Recordset.Fields (“Customer Id”) = txtCustID.txt orddate = now ‘date mm-dd-yy and hh:mm:ss datOrder.Recordset.Fields(“Date of Order”) = Month(orddate) & “-” & Day(orddate) & “-” & Year(orddate) datOrder.Recordset.Update Omit code in brackets if default value is set in ACCESS
Msgbox • MsgBox “prompt” • Default is the vbOKonly button • Same sa MsgBox (“prompt”, vbOKOnly) • MsgBox(“prompt”, VbOKOnly, “window title”) • Can display variables in the “prompt” • Msgbox (“this is a prompt ” & txtSSN.text) • MsgBox (“prompt ” & intNumber & “ the end”)
Msgbox • If you want to use other buttons: • vbYesNoCancel; vbYesNo; vbRetryCancel • need to Dim Response as Integer • response = MsgBox(“prompt”, vbYesNo, “title”) • Response = 6 for Yes; 7 for no; 3 for cancel • Can combine icon and buttons: • response = MsgBox(“prompt”, vbInformation + vbYesNo, “title”) • Options for icons: • vbCritical • vbQuestion