# Connect to two tables in one form

**URL:** <https://forum.servoy.com/t/connect-to-two-tables-in-one-form/15816>\
**Category:** Classic Servoy\
**Created:** [April 16, 2012, 12:59am UTC](https://forum.servoy.com/t/connect-to-two-tables-in-one-form/15816 "2012-04-16T00:59:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fgsmartest](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@fgsmartest](https://forum.servoy.com/u/fgsmartest)\
**Post date:** [April 16, 2012, 12:59am UTC](https://forum.servoy.com/t/connect-to-two-tables-in-one-form/15816/1 "2012-04-16T00:59:54Z")

</div>

I have a form that is attached to one table in my database but need a value from another table to perform a calculation. how do i write a query to that other table in the same database while the form is still attached to the other table.

---

<div class="post-metadata">

**Author:** ![Kahuna](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@Kahuna](https://forum.servoy.com/u/Kahuna)\
**Post date:** [April 16, 2012, 7:58am UTC](https://forum.servoy.com/t/connect-to-two-tables-in-one-form/15816/2 "2012-04-16T07:58:11Z")

</div>

Did you consider creating a relationship between the two tables, and displaying the value through that relationship?

If there is no direct relationship then perhaps harvesting the value through getDataSetByQuery is a better bet?

---

<div class="post-metadata">

**Author:** ![fgsmartest](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@fgsmartest](https://forum.servoy.com/u/fgsmartest)\
**Post date:** [April 16, 2012, 10:28am UTC](https://forum.servoy.com/t/connect-to-two-tables-in-one-form/15816/3 "2012-04-16T10:28:08Z")

</div>

thanks, can i get a code sample to access it through the relationship please?

---

<div class="post-metadata">

**Author:** ![Kahuna](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@Kahuna](https://forum.servoy.com/u/Kahuna)\
**Post date:** [April 16, 2012, 10:41am UTC](https://forum.servoy.com/t/connect-to-two-tables-in-one-form/15816/4 "2012-04-16T10:41:31Z")

</div>

Create your relationship TableA.your\_fieldNameA = TableB.your\_fieldNameB as (say) ‘tableA\_to\_tableB’

In your form place a field on the form and for its datasource use tableA\_to\_tableB.yourRelatedColumnName

This available in the drop-down where you are asked to determine the data source for a field (in properties) - you should be able to see your relationship and select it, then select the column from it. If the relationship is not visible in this drop-down it usually means that Servoy is expecting a global relationship. Sounds more difficult than it is so just think it through.

This should display your data from the second table.

You may need to use a global variable as one side of your relationship depending on what the link is you want. Search the forum for creating a global relationship. But once you have the relationship use the code above to display it (add your own table and column names of course ;-})

Best of luck

---

<div class="post-metadata">

**Author:** ![fgsmartest](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@fgsmartest](https://forum.servoy.com/u/fgsmartest)\
**Post date:** [April 16, 2012, 3:58pm UTC](https://forum.servoy.com/t/connect-to-two-tables-in-one-form/15816/5 "2012-04-16T15:58:20Z")

</div>

thanks, will try it
