473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Populating unbound form from VBA

doma23
107 100+
I was populating forms quite simple before I normalized my tables. I've had this:

Client_____Country___Period___Prod1___Prod2___Prod 32
Microsoft__Germany___ 2011 ___ 45 ___ 60 ___ 85

Client, Country and Period would give unique record, so I would use something like this code:

Expand|Select|Wrap|Line Numbers
  1. rs.Open "tblData", cn, adOpenStatic, adLockOptimistic
  2. sCriteria = "(([ClientID]='" & Form_frmMain.cmbClient & "') AND (" & "[CountryID]='" & Form_frmMain.cmbCountry & "') AND (" & "[Period]='" & Form_frmMain.cmbPeriod & "'))"
  3. rs.Filter = sCriteria
  4.  
  5. Form_frmMain.txtProduct1 = rs![Product1]
  6. Form_frmMain.txtProduct2 = rs![Product2]
  7. ...
  8. ...
  9. Form_frmMain.txtProduct32 = rs![Product32]
And that worked fine, but after normalization, I have this table:

ClientID__CountryID__Period__ProductID__Sales
___1_________2_______2011_______1_______ 45
___1_________2_______2011_______2_______ 60
...
...
___1_________2_______2011_______32______ 60

So now my code from above can't work, because Client, Country and Period will not give unique combination.
I would need to filter it further by the Product to find the unique record.

But since there is let's say 32 products, I would need to use the filter criteria 32 times (for each product once), instead of just once for all products like I was doing before.
I suppose it would have significant influence on execution speed as well?

The second problem is that now I have ProductID which I need to match, but on the form I have text labels that show which product it is and textboxes that represent revenue for each of these products.
So, how do I match the ProductID in the table and Product shown on form?

Expand|Select|Wrap|Line Numbers
  1. rs.Filter = "(([ClientID]='" & Form_frmMain.cmbClient & "') " & _
  2. "AND (" & "[CountryID]='" & Form_frmMain.cmbCountry & "') " & _
  3. "AND (" & "[Period]='" & Form_frmMain.cmbPeriod & "'))" & _
  4. "AND (" & "[ProductID]='" & Form_frmMain.? & "'))"
Obviously with the comboxes I guess I can do it (not tested yet) as they are bound in row source to ID value, but they show text description. But how to do it with the ProductsID column when all I have on the form is label showing text description of the product and textbox showing the value of sales for that product?

I hope I made myself clear to some degree.

So what is the right way to do this?

BTW. I know I could use DLOOKUP to populate it, but I've read somwehere it's not the best way since it has effect on the speed of execution so much.

Thanks!
Nov 17 '11 #1
5 2363
sierra7
446 Expert 256MB
Hi,
You cannot use Unbound forms in this senario, other than to display only one record at a time.Your data will now appear as a list down the page. You cannot spread it accross the page like you might have done with 'repeat fields' before normalising.

The best you might do is to create say three or four subforms side-by-side. The first would filter for ProductID 1-10, the second 11-20 etc. The main form would hold Company, Country & Period. Each of the subforms would have to link through these three fields.

Strictly speaking your data is still not Normalised.
S7
Nov 17 '11 #2
doma23
107 100+
Well, not necessarily. Like I've already mentioned, I can just use different filter criteria in VBA 32 times (to match the number of products), and that works, tested.

Here is the sample code (observe bolded code):
Expand|Select|Wrap|Line Numbers
  1. If DCount("*", "[tblRevenues]", _
  2.     "(([ClientID]=" & Form_frmMain.cmbClient & ") AND (" & "[CountryID]=" & Form_frmMain.cmbCountry & ") AND (" & "[Period]='" & Form_frmMain.cmbPeriod & "') AND (" & "[ProductID]=1))") > 0 Then
  3.     rs.Filter = "( ([ClientID]=" & Form_frmMain.cmbClient & ") AND (" & "[CountryID]=" & Form_frmMain.cmbCountry & ") AND (" & "[Period]=" & Form_frmMain.cmbPeriod & ") AND (" & "[ProductID]='1') )"
  4.      Form_frmMain.txtProduct1 = rs![Revenue]
  5. End If
  6.  
  7. If DCount("*", "[tblRevenues]", _
  8.     "(([ClientID]=" & Form_frmMain.cmbClient & ") AND (" & "[CountryID]=" & Form_frmMain.cmbCountry & ") AND (" & "[Period]='" & Form_frmMain.cmbPeriod & "') AND (" & "[ProductID]=2))") > 0 Then
  9.     rs.Filter = "( ([ClientID]=" & Form_frmMain.cmbClient & ") AND (" & "[CountryID]=" & Form_frmMain.cmbCountry & ") AND (" & "[Period]=" & Form_frmMain.cmbPeriod & ") AND (" & "[ProductID]='2') )"
  10.      Form_frmMain.txtProduct2 = rs![Revenue]
  11. End If
  12.  
And now, I do this for another 30 times.
DCount is being used in case there is no data for specific Company, Country, Period and Product. So only textboxes with Products that have the value in the database will be populated.
I would use similar logic when writing procedure for saving modified and new data.

The code can be probably further optimized by using loop to save the data about whether records exist in variables and then use another loop to assign the value to the fields. I might think about that later, depending on whether I will have the time to do it. (I'm under time constrain to finish the tool).

I'm still wondering though if there is more efficient way to do this, as I would like to now how to properly manipulate normalized data.
My code might work fine in the end, but it doesn't really look proper to me.

BTW. I have one frmMain, on which man can find comboboxes Client, Country and Period, and then I have ona tab control, which have DATA tab and REPORTING tab.
On DATA tab I have all the textboxes for the various products revenues. So I'm populating these textboxes when user wants to view/modify data, and when user press SAVE button, I need to save the modified data or add the new data in the database (depending on whether the record exists or not).

I really don't understand how would creating three or four subforms help me? That would change only visual layout of the data on the form.

And why do you think it's still not normalized?
Nov 17 '11 #3
NeoPa
32,556 Expert Mod 16PB
I don't want to get too deeply involved with this as there seem to be far too many ideas here that I would not expect to see in a properly defined database. I'm sorry if that sounds harsh Doma. I only posted because you seemed under the impression you had normalised your database. In case it helps see Database Normalisation and Table structures.

In Access it is generally a sign of something wrong with the database design if someone asks for help populating a form. It's the reverse of the way Access is designed to work. It's possible to do, and many people fall into that trap when they come from other programming environments, but it's like owning a luxury car and having the chauffer drive it beside you while you walk.

As I said, I don't want to get involved deeply here, but I felt I had at least to try to warn you away from some of what you're doing. I'll leave it with you.
Nov 17 '11 #4
doma23
107 100+
The thing is NeoPa, that I've read quite some about normalization, and the whole time I was under impression that I'm doing things by the book.
I designed the tables and made the relationships, and I gave it quite some thought in an effort to do it properly and normalize it.
And now in the last couple of days I'm trying to build the form and the queries to manipulate these data in a way that the users will expect to do it, and I find it quite complicated, not impossible, but very complex.
And I just don't know if I did something wrong, or if it's the nature of the data.

NeoPa, I would really appreciate if you could help me here man. I know it would take you some time, and I'm willing to pay you something for your effort if needed.
If you agree I can even send you the whole database, so you can see the design of the tables and the relationships among them.
I really think it wouldn't take you much time to understand it as the project is quite simple IMO.
I'm doing this project for the company I work for, and I know they don't care if it's normalized or not as long as it works. But I do, and I so badly want to understand it, for myself. I will not have peace until I do so. :(
Please let me know.

Btw. here is the relationship view, if you would just take a look I would really be happy.
I'll answer whatever questions you may have.
P.S. CategoryID and ProductLineID are duplicated on purpose, because I need to preserve these information, as they will change over time.*

Nov 18 '11 #5
NeoPa
32,556 Expert Mod 16PB
I expect you completely underestimate what it is you're asking for here Doma, but I like your attitude.

I will send a PM with some details and we can possibly chat at least. You can send me the db too if you like. This is too complicated to do via a forum page but if we get to talk it will help to have the existing project in front of me. You will need to follow the guidance in this link though to ensure all is done properly (Attach Database (or other work)).

I checked your profile, and I hope your English is as good as most of the Germans I speak with, as my German is less than rudimentary.
Nov 18 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tim Marshall | last post by:
This is an embarrassing question to which I should know the answer, but I am not 100% sure of myself. My applications, whether they are Jet or Oracle usually deal with reporting on existing apps...
1
by: huela | last post by:
Somehow i have to display the recordset in unbound form When i use the following display the data in recordset to unbound form. I have a question: i can only display the last record data, all...
4
by: Pierre | last post by:
Hi all, To ease load on a network i close automatically form open with a timer reset by user actions. If the time is expired i go through the collections of form and table and close all those...
11
by: TD | last post by:
I'm looking for input into my decision to switch to ADO and unbound forms. I get tired of having to block all of the ways a user can unknowingly save a record, like using the PageUp, PageDown keys...
6
by: Dave G | last post by:
I am writing a function to fill in the data in an unbound form. I have a table with field names of 1, 2, 3 etc up to approx 100. I have an unbound form with fields called 1, 2 3 etc. I gave...
6
by: Volker Neurath | last post by:
Hi all, I have a Problem with combobox-property "NotInList" and an unbound Form. The situation: On my main form i have three comboboxes for data-exchange (here: Names of distributor,...
7
by: DeZZar | last post by:
Hi all, Unfortunately I am quite a novice with Access!! I've created a number of data bases for my work however becuase my skills are limited to really built in functionality and wizards my...
7
by: troy_lee | last post by:
I have an unbound form. I have an Add New procedure that saves a new record to the database. What is the best way to update an existing record? In other words, clicking my "Submit record" button...
4
by: dizzydangler | last post by:
Hi all, I am a new Access user and just starting to get my head around some of the basic concepts, so please take it easy on me :) My company has been managing client records on excel, and I’m in...
2
by: Ruth Barn | last post by:
The issue is that the calculated text box does not actually requery until each of the associated listboxes have been clicked into and exited. I have an unbound form that looks up various pieces of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.