How to create a program to convert unit measurements

1.4k views Asked by At

Using Microsoft access, visual basic.

I'm having a big problem doing this task. What I have done: Created a table on access where I have put measurements in (from meters): mile = 10000meters, nautic mile = 1862meters, English mile=1652, kilometers = 1000 meters and all the way down to Millimeters.

What I have created for input: 1 box takes an Integer to be converted and a 1 box specified with an initial unit.

What I have created for Output: 1 box shows the Integer of result with 1 box specified the chosen unit of the output.

Can anyone please, please help me with the codes? UserLayout

conversionTable

2

There are 2 answers

0
Gustav On

For both comboboxes, bind them to column 2, faktorTilMeter, and set the ColumnWidths to, say: 2,542cm;0cm.

Then, assign this expression as ControlSource for your output textbox:

=TextboxInput/ComboboxFrom*ComboboxTo
0
ashleedawg On

Honestly I'd never really noticed the CONVERT function until today but here's a quick demo of how I'd slap together a "conversion tool" in Excel.

If you want to do the same thing in Access, the premise is the same, but it will be a bit more work since you'll have to design the form from scratch instead of using a worksheet, which is kind of meant for this kind of job.


Using Excel functions in Access

Before you are able to use Excel's CONVERT function in Access, you'll need to reference the Microsoft Excel Object Library.

  1. In Access, open any VBA Module.
  2. GoTools > References
  3. Check the box next to Microsoft Excel 16.0 Object Library. (The version number will vary if you have an older version of Office.)

Then you can call most Excel functions from Access VBA or queries with WorksheetFunction (the same way you would use them in Excel VBA).

For example:

MsgBox WorksheetFunction.Convert(3.7, "m", "ft")

...displays a message box with the number of feet in 3.7 metres.

The calculations will be the easy part; a couple lines of VBA in the On Change or On Exit events will trigger the calculation.

The most time-consuming part will likely be perfecting the placement and formatting of the controls on the form, which is by no means difficult (and there are several tutorials online that can provide the basics if necessary.)

Lastly, keep in mind that there are no doubt a plethora of existing conversion tools available for free download with a little Googling... (I'm confident that you're not the first person who wanted to use MS Office to convert measurements.)


More Information:

You can download the demo xlsx used above from JumpShare here.