Cardbox > Forums > Cardbox Talk > "Macro help needed again, so sorry"
Macro help needed again, so sorry
Please can anyone help with the below |
Current user: [none] |
| Posted By | Post |
|---|---|
Alpha | 26-Jun-2008 14:12 I am looking for help to write a macro, let me explain. I have a database with 3 columns, quantity, cost each and total. I need a macro that could take the number in the quantity column multiply it by the cost each column and provide a total. Is this possible? I do have this macro in place for adding figures in a field, but have no idea how to modify it. total=0 |
Charles Welling | 27-Jun-2008 08:32 Alpha, here's the modified macro. You'll have to change the field "NONAME1" to your own field name, and you must tell the macro which character(s) you use as delimiters for your columns. 4, 8, 32 the macro will work correctly. Charles splitchar=", " if Fields("NONAME1").Textlength > 0 then lines=Split(Fields("NONAME1"),CHR(10)) EditRecord |
Alpha | 27-Jun-2008 12:28 Charles, thankyou for your assistance, however I am not very educated when it comes to macros, can I be cheeky and ask you to help me complete it with the below information. My quantity field is called Q2 Can you help apply them to the macro? |
Charles Welling | 27-Jun-2008 13:54 In this case the macro is not going to work at all. You wrote that you had three COLUMNS, not three FIELDS. What the above macro does, is splitting a single field into its constituent parts and do some calculations with these parts. In the end the parts are joined again. If your fields contain only one amount/quantity, then the correct macro is much more simple, because you can refer to the field as a whole, no need for splitting: EditRecord When you are already editing the record, and I suppose you would be, omit the line "EditRecord". On the other hand, if each field contains more than one line, the macro becomes more complicated. I'm willing to help out, but not before I'm sure I have all the relevant details. So, if the new macro isn't the right one, please tell me EXACTLY how your data is organised. Charles |
Alpha | 1-Jul-2008 10:50 Charles, thankyou, I will attempt to explain as clearly as I can. We have a database which comprises of 3 fields, it is designed to calculate a quick quote to a client The first field is to input a quantity and we have named the field Q2, we would be inputing more than one figure in this field The second field is a cost each field which we named Q5, as above this will have more than one figure in it Finally we have a Total field, named Q6 See below for how we intend to use the fields Quantity Cost Each Total 1 £25.00 £25.00 Is this acheivable? |
Charles Welling | 1-Jul-2008 13:29 Well now, that is clear as cristal. Below you'll find the macro, and I'll explain what it does. Assuming that you are editing the record, it first saves it. For the macro to work correctly (and for your quote to be correct), there must be an equal number of lines in Q2 and Q5. There may not be empty lines at the end of the fields, and saving the record simply strips the fields of any empty lines. Then the macro start editing again. ---------- SaveRecord quantities=split(Fields("Q2"),chr(10)) lines=0 for counter = 1 to lines Fields("Q6")=totalstring SaveRecord |
Charles Welling | 1-Jul-2008 13:49 I just remembered that you would like to have your amounts nicely formatted. ---- totalstring=totalstring & "£ " & FormatNumber(quantities(counter-1)*costs(counter-1),2) & vbCrLf |
Alpha | 2-Jul-2008 11:53 Charles, this works an absolute treat, and I thank you for this. Am i to late to throw one more thing into the mix. I have been asked to create 1 final field in this format that you kindly wrote the macro for, this field is called Q62. The purpose of this field is to provide a grand total for all the figures in field Q6. Is there a line you can add to the macro created below that will finally add the figures in Q6 and display the result in Q62? Your help is much appreciated |
Alpha | 2-Jul-2008 12:03 And one last thing Charles, how can I get the macro to run after pressing save, at the moment I edit the record, input the data and then have to click tools, run macro then the macro. Can the macro be attached to the save button? |
Charles Welling | 3-Jul-2008 08:26 Here is your revised macro. You can attach this macro to the Save command as follows: Edit the native format (Edit > Format > Edit) Do the same for the CTRL-s keystroke: Save the format. What happens now is that when a user makes a new record or edits an existing record the macro will run and calculate the quote. ------ SaveRecord lines=0 for counter = 1 to lines Fields("Q6")=totalstring SaveRecord ------ Charles |
Alpha | 3-Jul-2008 09:02 Thank you Charles, it works extremely well, I know you have shown me how to do this but the total box Q62 is showing the total as e.g. £200 not £200.00, is this easy to fix? |
Charles Welling | 3-Jul-2008 09:29 Yes, off course. I forgot. Fields("Q62")="£ " & FormatNumber(grandtotal,2) |
Charles Welling | 3-Jul-2008 10:13 Here's a solution for "Save as New". If you have already attached the macro to the buttons/menus and keystrokes, please add ",1" to the name of the macro. So, if your macro is called "save" and you told Cardbox to run "save" when the user clicks the button or menu (or CTRL-S) then it should now be: "save,1" (omit the ""). Attach the macro to the SaveAsNew button as well, but now add ",2" (play macro "save,2"). The macro will now be aware of the button/menu the user has used, and it will either save the record or save it as a new record. Replace your macro by the following: do do quantities=split(Fields("Q2"),chr(10)) lines=0 for counter = 1 to lines Fields("Q6")=totalstring if commandline = "1" then |
Egbert | 3-Jul-2008 10:41 From Egbert to Charles & Alpha: a separate forum is available for these macro problems |