I need help with the following H.W. problem. I have done everything except the instructions I numbered. Please help!
A furniture manufacturer makes two types of furniture—chairs and sofas.
The cost per chair is $350, the cost per sofa is $925, and the sales tax rate is 5%.
Write a Visual Basic program to create an invoice form for an order.
After the data on the left side of the form are entered, the user can display an invoice in a list box by pressing the Process Order button.
The user can click on the Clear Order Form button to clear all text boxes and the list box, and can click on the Quit button to exit the program.
The invoice number consists of the capitalized first two letters of the customer’s last name, followed by the last four digits of the zip code.
The customer name is input with the last name first, followed by a comma, a space, and the first name. However, the name is displayed in the invoice in the proper order.
The generation of the invoice number and the reordering of the first and last names should be carried out by Function procedures.
Seeing as this is homework and you haven't provided any code to show what effort you have made on your own, I'm not going to provide any specific answers, but hopefully I will try to point you in the right direction.
Your first 2 numbered items look to be variations on the same theme... string manipulation. Assuming you have the customer's address information from the order form, you just need to write 2 separate function to take the parts of the name and address, take the data you need and return the value (which covers your 3rd item).
To get parts of the name and address to generate the invoice number, you need to think about using the Left() and Right() functions.
Something like:
Dim first as String, last as String, word as String
word = "Foo"
first = Left(word, 1)
last = Right(word, 1)
Debug.Print(first) 'prints "F"
Debug.Print(last) 'prints "o"
Once you get the parts you need, then you just need to worry about joining the parts together in the order you want. The concatenation operator for strings is &. So using the above example, it would go something like:
Dim concat as String
concat = first & last
Debug.Print(concat) 'prints "Fo"
Your final item, using a Function procedure to generate the desired values, is very easily google-able (is that even a word). The syntax is very simple, so here's a quick example of a common function that is not built into VB6:
Private Function IsOdd(value as Integer) As Boolean
If (value Mod 2) = 0 Then 'determines of value is an odd or even by checking
' if the value divided by 2 has a remainder or not
' (aka Mod operator)
IsOdd = False ' if remainder is 0, set IsOdd to False
Else
IsOdd = True ' otherwise set IsOdd to True
End If
End Function
Hopefully this gets you going in the right direction.
Related
I have the following block of code that iterates through the fields of each table and adds the fields of the current table respectively in order to create a number of tableboxes.
'iterate through every table
For i=1 To arrTCount
'the arrFF array holds the names of the fields of each table
arrFF = Split(arrFields(i), ", ")
arrFFCount = UBound(arrFF)
'create a tablebox
Set TB = ActiveDocument.Sheets("Main").CreateTableBox
'iterate through the fields of the array
For j=0 to (arrFFCount - 1)
'add the field to the tablebox
TB.AddField arrFF(j)
'Msgbox(arrFF(j))
Next
Set tboxprop = TB.GetProperties
tboxprop.Layout.Frame.ObjectId = "TB" + CStr(i)
TB.SetProperties tboxprop
Next
The above code creates the tableboxes, but with one field less every time (the last one is missing). If I change the For loop from For j=0 To (arrFFCount - 1) to For j=0 To (arrFFCount) it creates empty tableboxes and seems to execute forever. Regarding this change, I tested the field names with the Msgbox(arrFF(j)) command and it shows me the correct field names as I want them to be in the tableboxes in the UI of QlikView.
Does anybody have an idea of what seems to be the problem here? Can I do this in a different way?
To clarify the situation here and what I have tested so far, I have 11 tables to make tableboxes of and I have tried with just one of them or some of them. The result I am seeing with the code is on the left and what I am expecting to see is on the right of the following image. Please note that the number of fields vary for each table and the image has just one of them as an example.
1st year coding student and have a project in my visual basics class.
Create a simple calculator that will multiply, divide, subtract and add. Thing is we are not allowed to use any decision structures or loops, only sequential style coding structure.
I am struggling with storing, passing, and utilizing the selected mathematical operator later in the program.
When coding equations, what data type are operators considered?
For example in 4-1=3 is - considered a string?
How would one go about storing this value and passing it to another section of the program then converting that to a form you could plug into a formula, all without using a decision structure or loop?
for example:
1) User clicks on "1" button a string value of 1 is stored in a label as a string.
2) The user clicks the the "+" button and a + is stored (not sure what data type to use here for later needs) in a label.
3) Then user clicks "1" button a 1 is stored in a label as a string.
4) User clicks the "=" button.
5) The = button event handler executes code converting both the "1" strings to integer variables and the formula should represent IntResult= IntvariableOne + intVariableTwo. but since the operator may not always be + and no decision structure can be used. how can this be coded in a way that uses a variable to store the operator and complete the processing correctly in a formula?
In the most simple terms it would be equivalent to something like:
intResult= intvariableOne, Operatorvarible, intVariableTwo
Like I said I am new to coding so I apologize if this is a dumb question or completely wrong approach.
any info is appreciated.
thanks
It's a cheat, but try using Eval:
Eval("4-1") ' returns 3
Reading your post I was thinking about using the "CallByName" function.
But you can also try something more elegant with some kind of "oop" (reaching some of the limits offered by the old VB6) :
Create a file "ICalculator.cls" :
'
' Defines the "ICalculator" interface.
'
Option Explicit
Public Function CalcProcess(value1 As Integer, value2 As Integer)
End Function
Create a file named "CalculatorAdd.cls" that will take "additions" in charge:
'
' This "class" will implement "ICalculator" inteface to manage additions.
'
Option Explicit
Implements ICalculator
Private Function ICalculator_CalcProcess(value1 As Integer, value2 As Integer)
ICalculator_CalcProcess = value1 + value2
End Function
An then, an example of how it works :
Sub Test()
Dim value1 As Integer
Dim value2 As Integer
value1 = 1
value2 = 2
' Global object :
Dim objCalculator As ICalculator
...
' The object is set to "Addition" in the onclick event of the "add" button :
Set objCalculator = New CalculatorAdd
...
' The process is done in the onclick event of the "equal" button
value1 = objCalculator.CalcProcess(value1, value2)
...
' You can also have :
'Set objCalculator = New CalculatorSub
'value1 = objCalculator.CalcProcess(value1, value2)
'etc...
End Sub
You can create classes for each operation "add, sub, divide, multiply" and set your global variable objCalculator according to the button pressed by the user.
That's just a beginning, you'll have to put the whole logic of a calculator in place...
I am looking for someone who uses his/her high maths expertise to create an algorithm to deliver pseudo-random TAN codes. The algorithm starts off with a seed and is able to generate any n-th code. Repeatedly retrieving a code for the same position returns the same value. Repetition of the same value at different position: not before 10^12 values or better.
It is able to generate the next code based on the last one or on the position/index provided as input. The code itself is a string consisting of a set of chars.
The algorithm is able to check a given code if it is a valid code of the sequence created by seed.
The algorithm is able to save and restore its state (serializable). It does not precalculate a list of keys. Repetition of codes is ok,
Some terms:
Position: whatever needed to indicate the n-th code in the sequence. Integer, structure, whatever is needed.
Alphabet, a set of one or many of those:
Lower: all lower case chars a-z
Upper, all upper case char A-Z
Digits, 0-9
Special, non-alfanum chars
Safe, eliminates dangerous chars like ilIO0S5
I could imagine something like this in pseudo-code:
Class TANgen.
Constructor (seed, alphabet, codelength)
// this initializes the algorithm to be able to start. It shuld assume that we will retrieve one code after another, so the class memorizes how many codes have already been generated.
string function get ()
string function get (position)
// get the next code or the code at position.
string function get_next (position)
string function get_next (code)
// both functions get the next code, following the one given either by a position or a code
string[] function get (position, count)
string[] function get (code, count)
// get count codes starting with the one given by either a position or a code
position function validate (code)
// checks the code and returns its position or null if not valid
boolean function validate (code, position)
// wrapper for validate() != null
position function validate (code, position, windowsize)
// returns the position of a code if found at position, where position is the middle position of a sliding window. So like code in (code[position-windowsize],…,code[position-1],code[position],code[position+1],…,code[position+windowsize])
function save()
function load()
Maybe someone has already done that or knows where I can start off.
Any help or pointers to source is highly appreciated.
Thank you.
I have a fairly complex look of code where I am looking through multiple control variables.
I am getting an error 'Invalid 'for' loop control variable
the line in questions is
for w(1) = 32 to 127
I am more familiar with VBA where I would have zero problem with this statement.
I'm guessing it has something to do with the fact that i will be looping through w(1),w(2),w(3) etc. in the same tree. I initialize the variable as dim x(10) but have also tried dim w() , dim w() redim w(10)
Any thoughts? its a fairly critical aspect of the script; as such I am unwilling to swap out all my w 1,2... for individual variables
Thoughts?
EDIT:
As per comments I should clarify a Few things:
Essentially there is a alpha numeric association with an ID in a system that I am working with which I was not handed down the key too. So I have a multi-dimensional array of rates that are used for multiplying out costs.
What I am doing is working backwards through invoices and matching a material with very subtle differences that have different pricings.
For simplicity sake, say theres a 2 dimensional material where AA, AB, ... A9 are all priced through several multiplication factors in what would just be a 2x2 grid. So maintaining a pivot point based on the position in string is very important. For this code you could take tier to mean how many characters in the string (aka how complex the composition of the material):
dim x(), w()
for tier = 1 to 2
for w(1) = 32 to 127
x(1)= chr(w(1))
If tier = 2 then
for w(2)= 32 to 127
X(2)=chr(w(2))
next
end if
str = ""
for y = 1 to (tier)
str = trim(str & x(y))
next
'''msgbox str 'debug
next
end if
str = ""
for y = 1 to (tier)
str = trim(str & x(y))
next
'' msgbox str ' debug
next 'tier
This is just an excerpt i pulled to get a basic idea of the structure w/o any calculations. this is in essence what is not working
The error is quite clear, you cannot use an Array as the control variable. The definition in For...Next Statement is even clearer;
Numeric variable used as a loop counter. The variable cannot be an array element or an element of a user-defined type.
This is one of the key differences between VBA and VBScript.
You won't loop through x(1),x(2)...on what you write it's going like this 32(1),33(1)....what type it's your w(1) and how you define him?
I currently have a worksheet that I have multiple people filling out every day. There are 4 columns that the users fill out: C, E, H, & J (all numerical values, one row per day of the month.)
The users fill in C, E, & H every day no matter what, but a lot of days there is no value to put in column J. I need the value in J to be set to 0 if the user doesn't enter anything. Of course it would be easier to just have the users enter 0, but I'm working with a complicated group of people here.
Anyway, I want to use a macro that runs automatically when the user clicks the save button (before it actually saves, of course), and have it do the following: (I am more familiar with php, so I'm just typing this out how I'm familiar - I'm sure my syntax is incorrect)
Foreach Row
If "column A" != "" {
If "column J" != "" {
//Everything is good, continue on...
} else {
CurrentRow.ColumnJ.value == 0
}//Value has been set - continue loop
}
//column A is blank, this day hasn't come yet - quit looping here
End Foreach
If anyone could help me out with this I'd appreciate it. With some research, this is what I've come up with so far, and now I'm stuck…
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim curCell As Range
'Labor Flow Sheet'.Select
For Each curCell in Range( ???? )
If curCell.Value = "" Then
???????
End If
Next curCell
End Sub
Thanks in advance!
See this link about finding the right range, and as for the question marks inside the If statement, you would want to put
curCell.Value = 0
For the question marks in your statement
For Each curCell in Range( ???? )
Solution 1:
To find the full range you're working with, you'll need to use a column that is filled out each day. You mentioned columns C, E, and H were filled out every day. Using one of those columns (let's pick C for the example here), you could find the range by using the .end method. This goes out either up down left or right from a range until it doesn't find any data. So,
Range("J1", Range("C1").End(xlDown)).Select
will select all cells from J1 (or whatever column is the last in your sheet) to the bottom-most cell containing data in column C, automatically.
Solution 2:
Manually put in the range. For example, to choose A1 to J300:
Range("A1", "J300").Select