473,664 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VBA code to look in table and open form

16 New Member
Hi everyone,

I have another question for you all. I have a form A in which I enter a number # and this number is stored in a table 1. In this same form A, I want to add a button. When I click this button I want it to look in a table 2 to see if the number # I entered is already there. If it is not there, I want form B to open and have the number # already in the field associated to it so I only have to fill in the rest. However, if it is already in the table 2(so it is already in the form B), I want it to open the form B and show all the fields that were previously entered including the number #.

Does someone know what VBA code I would need to enter in the Private Sub Button_Click()?

This is what I have now, but it does not work :
Expand|Select|Wrap|Line Numbers
  1. Private Sub RP_Click()
  2.  
  3. If DLookup("NoRecomPaiem", "TblRecomPaiement", "NoRecomPaiem='" & Me.NoRecom & "'") Is Null Then
  4.  
  5.     DoCmd.OpenForm "FrmRecomPaiement", , , , acFormAdd
  6.     Forms!FrmRecomPaiement!NoRecomPaiem = Me!NoRecom
  7.     Forms!FrmRecomPaiement!NoRecomPaiem = Me!NoRecom
  8.     Forms!FrmRecomPaiement!DateRecom = Me!DateRecom
  9.     Forms!FrmRecomPaiement!NoFacture = Me!NoFacture
  10.  
  11.         DoCmd.Close acForm, "FrmFacture"
  12.  
  13. Else
  14.  
  15.     Dim db As DAO.Database
  16.     Dim rst As DAO.Recordset
  17.  
  18.     On Error GoTo Err_RP_Click
  19.  
  20.     DoCmd.OpenForm "FrmRecomPaiement"
  21.  
  22.     Set rst = Forms!FrmRecomPaiement.Recordset.Clone
  23.  
  24.     rst.FindFirst "[NoRecomPaiem] = " & Me.NoRecom
  25.     Forms!FrmRecomPaiement.Bookmark = rst.Bookmark
  26.  
  27. Exit_RP_Click:
  28.         Exit Sub
  29.  
  30. Err_RP_Click:
  31.         MsgBox Err.description
  32.         Resume Exit_RP_Click
  33. End If
  34. End Sub
Aug 4 '11 #1
18 2029
NeoPa
32,568 Recognized Expert Moderator MVP
It would help for your question to match the code. A question with one set of names associated with code with a different set of names just makes life more complicated.

Generally, when you have an issue like this to deal with, I would consider opening the new form (Form B in your question) and, using the .Recordset of the form, attempt to navigate to the specified record. If that fails then simply set the .DefaultValue property of the relevant control to match the number selected from your old form (Form A in the question). If you prefer, you can also move the recordset to the New Record position at this point too.
Aug 4 '11 #2
David Desormeau
16 New Member
I tried to simplify it without all the names and stuff, but if it didn't help, the number # is equivalent to NoRecom (it is called NoRecomPaiem in some forms and tables). The table A is TblFacture and table B is TblRecomPaiemen t. For the forms, form A is FrmFacture and form B is FrmRecomPaiemen t.
Aug 5 '11 #3
NeoPa
32,568 Recognized Expert Moderator MVP
That makes a certain sense David (so I understand where you're coming from). It simply doesn't work that way though. It makes it harder instead. Easier is to be able to reference across easily and match one to the other. No harm this time, but probably best to remember for future questions :-)

Your last post didn't comment on whether you had managed to get it to work following my instructions. We are interested to hear if it did, or alternatively if you need further assistance.
Aug 5 '11 #4
David Desormeau
16 New Member
I'm not sure I fully understand what you said in your first post. If i set it as a default value, how will it know when to open the form to an old record that already has that number or when to open a new record and automatically write the information I already entered (RecomPaiem, NoFacture and DateRecom)?

Also, I don't know if the code I entered in my first post is wrong (I tried it with things I found on google), but it says that there is a type problem with my dlookup line (line 3). NoRecom and NoRecomPaiem are both number types in their respective tables. I even changed the name of NoRecom to NoRecomPaiem (because they are essentially the same thing) but nothing helped (I did change the NoRecom in the code too).
Aug 5 '11 #5
NeoPa
32,568 Recognized Expert Moderator MVP
David Desormeau:
I'm not sure I fully understand what you said in your first post. If i set it as a default value, how will it know when to open the form to an old record that already has that number or when to open a new record and automatically write the information I already entered (RecomPaiem, NoFacture and DateRecom)?
The DefaultValue property of a control doesn't pertain in any way to which record is currently selected. The only effect it will have is that when on the New Record, the control that it is the DefaultValue of will be set to that value (but importantly without flagging the record as having been changed). Essentially it sets its starting value for new records.

Lines #20 through #25 in your code are an example of moving the recordset of the new form to the required position (as stated in the second paragraph of post #2). It should work perfectly as long as [NoRecomPaiem] is a numeric field. As this code runs after checking for the existence of the record this should never fail.

David Desormeau:
Also, I don't know if the code I entered in my first post is wrong ...
From my perusal of the code I would say :
  1. Line #3 treats [NoRecomPaiem] as if it were a string whereas line #24 treats it as numeric. Only one of these can possibly be correct.
  2. Lines #6 through #9 set the .Value (The default property) of these four controls, instead of the .DefaultValue as I suggested. This may cause you complications.
  3. I see no further problems. You say it doesn't work, but give no further clue as to what went wrong. If you still have an issue with your code you need to repost the updated code and explain what went wrong where (Error message and line # are minimum requirements but don't stop there if more information is required).
Aug 5 '11 #6
David Desormeau
16 New Member
I barely changed the code. Like I said, I just changed the name of NoRecom to NoRecomPaiem like the other. This is what it gives :
Expand|Select|Wrap|Line Numbers
  1. Private Sub RP_Click()
  2.  
  3. If DLookup("NoRecomPaiem", "TblRecomPaiementDet", "TblRecomPaiementDet.NoRecomPaiem='" & Me.NoRecomPaiem & "'") Is Null Then
  4.  
  5.     DoCmd.OpenForm "FrmRecomPaiement", , , , acFormAdd
  6.     Forms!FrmRecomPaiement!NoRecomPaiem = Me!NoRecomPaiem
  7.     Forms!FrmRecomPaiement!DateRecom = Me!DateRecomPaiem
  8.     Forms!FrmRecomPaiement!NoFacture = Me!NoFacture
  9.  
  10.         DoCmd.Close acForm, "FrmFacture"
  11.  
  12. Else
  13.  
  14.     Dim db As DAO.Database
  15.     Dim rst As DAO.Recordset
  16.  
  17.     On Error GoTo Err_RP_Click
  18.  
  19.     DoCmd.OpenForm "FrmRecomPaiement"
  20.  
  21.     Set rst = Forms!FrmRecomPaiement.Recordset.Clone
  22.  
  23.     rst.FindFirst "[NoRecomPaiem] = " & Me.NoRecomPaiem
  24.     Forms!FrmRecomPaiement.Bookmark = rst.Bookmark
  25.  
  26. Exit_RP_Click:
  27.         Exit Sub
  28.  
  29. Err_RP_Click:
  30.         MsgBox Err.description
  31.         Resume Exit_RP_Click
  32. End If
  33. End Sub
  34.  
When I go in the form and click on the button called RP, this message shows up :



Which is french for :
Execution Error '3464':
Type of data incompatible in the expression of the criteria


When I click on debug it brings me to the line 3.
My first guess was that the two NoRecomPaiem where not of the same type in their tables, but they are both set to numeric.
Aug 5 '11 #7
NeoPa
32,568 Recognized Expert Moderator MVP
David:
My first guess was that the two NoRecomPaiem where not of the same type in their tables, but they are both set to numeric.
I'm not sure why you didn't understand my previous post. I expect dealing in a foreign language makes everything that (not very little) bit more complicated. That's an excellent excuse :-D I speak some French, but dealing with technical issues on a French forum site would be at least a massive struggle for me.

I will try again more explicitly (as I now have more direct information too from which I can be sure which is the correct version between lines #3 and #23 in your latest post).

Line #3 treats the value returned by Me.NoRecomPaiem as a string value (due to the quotes (') - See Quotes (') and Double-Quotes (") - Where and When to use them) whereas line #23 treats it correctly (as a numeric value).

A better version of line #3 maight be :
Expand|Select|Wrap|Line Numbers
  1. If DLookup("NoRecomPaiem", "TblRecomPaiementDet", "[NoRecomPaiem]=" & Me.NoRecomPaiem) Is Null Then
Aug 5 '11 #8
David Desormeau
16 New Member
Ok I think we're getting some where. The previous error is now gone, but now when I click the button another error message pops up :

Error '424':
Object required (The english version should look something like this I think)

The error is still in line 3. I don't have a clue what this error could mean.
Aug 5 '11 #9
NeoPa
32,568 Recognized Expert Moderator MVP
Can you post what your line #3 now looks like. I'm hoping it's changed but I have no information as to how it looks now.
Aug 5 '11 #10

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

Similar topics

1
5826
by: P | last post by:
Hi, Access 2002. I am trying to use the PrintOut function to print the current record of the current open form in landscape format to the default Windows printer. Any suggestion on how to set the printout to landscape mode? Thank you. P
7
2746
by: TDIOwa | last post by:
I have a form that has a specific date on it. I want to open another form that has the Active Control Calendar on it. I want to open this form to the specific date on the first form. I have searched this forum high and low to find a solution and have tried virtually everything mentioned, all to no avail. Some solutions even crash the computer. Does anyone have any ideas? Thanks TD
4
1960
by: yanjie.ma | last post by:
Hi, I've got a two part question on table and form design (sorry for the length but it takes a bit to explain). Our sales department uses a look-up table to help the them select the best equipment to use for a customer's needs. It basically consists of a table with the following setup: Equipment: Option1: Option2: Option3: Option 4: ... -------- ------- ------- -------- --------
14
25039
by: keri | last post by:
Hi, Simple version of the question..... How do I use the where clause of the open form command to show an account with a matching ID to be displayed when the form is opened? Eg. I select a record on a continuous form and click and edit button. The button runs the open form code. I want the APPID of the current record on the continous form to match the APPID of the record viewed
6
3529
by: Markus_989 | last post by:
I have a LOANS table that has a list of loan details for different borrowers. I have a main switchboard with a LOANSELECT combo box (that displays a list of borrower last names and loan numbers). I'd like to be able to have a user click on the say, RECEIVE PAYMENT button that will open the payments form. This form should open to the loan that was selected from the LOANSELECT box. This form also has a box on it to switch to different...
1
2327
by: Kevin | last post by:
The menu form has two options, one to create a new estimate, the second to open an existing estimate. Under option two is a list box containing all of the estimates. There are several different estimate categories however. The siginificance is, all of the estimates are in the same table, but there are different forms for viewing and editing each type. Each estimate record has a field for EstimateType. The user selects an estimate from...
1
1505
by: Ste | last post by:
Hi, I would like to have current date automatically enter into a form that is based on a table, so that from report I can tell how many records I have entered for the day. If I use date() on the data entry form, would I get the record changing the date whenever I happen to browse the data entry form? That I don't want to happen. What is the better way to keep track of the date on each record entering without manually inputting...
13
3190
by: smiler2505 | last post by:
Sub cmbEnt_NotInList(NewData As String, Response As Integer) Answer = MsgBox("add?", vbYesNo, "add?") If Answer = vbYes Then DoCmd.Close acForm, "frmOrd" CurrentDb().TableDefs("tblOrd").Fields("Ent").RowSource = CurrentDb().TableDefs("tblOrd").Fields("Ent").RowSource & ";" & NewData Else
5
7636
by: billa856 | last post by:
Hi, My project is in MS Access 2002. In that I want to open one form multiple times. I put one button on my main form. Now whenever I click on that button than form will be open. Now when I click again on that same button than it will open same form again in seperate window. But instead of that already open form comes.and so i couldn't open that form multiple times. Already open form must be remain as it is even if I click on that button...
6
3063
by: Adam Tippelt | last post by:
I've got a form that allows users to "save the current record to drafts" if they haven't finished writing all the data for that record. This saves it in it's current state, which puts it in a temporary table. The record is also tagged with the name of the user who created it. What I'm trying to do is to force the form to open up and load this record when the form is loaded/opened. The structure of the database only allows users to have one...
0
8438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.