473,385 Members | 1,908 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,385 software developers and data experts.

Using visual basic to populate fields of a form from another table?

10
I have written an Access database that our department uses to store and analyze information on particular jobs. Because our company runs on an enormous SQL DB, I thought it would save some time if the basic job details (name, location, salesperson, etc) which is already stored in the SQL DB could be loaded onto one of my forms at the click of a button. The form needs this information (and more that has to be manually entered) to be complete. I wrote an SQL view and linked it into my Access DB. My idea is to be able to, when entering new jobs, type in a job number on my form, and then at the click of a button, populate several other fields from information already stored in the view. However, I'm getting run-time error 424 "object not found" - even though all relevant fields have been included on my SELECT statement in my form the appropriate INNER JOIN has been done. Here's how I'm doing it:

Expand|Select|Wrap|Line Numbers
  1. Private Sub LoadInfo_Click()
  2. ' Pulls information out of SQL view.
  3.     Me!SalesManager = dbo_vwBookingInfo.SM
  4.     Me!ProjectManager = dbo_vwBookingInfo.PM
Access does recognize the view as I've successfully written a query with it. Does anybody know how to accomplish what I'm trying to do? Not all jobs in the SQL view should go into my Access table, which is why I'm trying to have it only load information one job at a time on command...
Sep 6 '06 #1
13 7622
Selesti
10
Instead of using visual basic, I tried writing a query to search for the information I needed, and then using a macro to have the field on the form = the field on the query.

This is the error I am now getting:

Sep 7 '06 #2
MMcCarthy
14,534 Expert Mod 8TB
You cannot reference the view directly. You need to use something like a recordset. As this returns all records in the view and you can only load one record at a time into the table you will need to set criteria.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub LoadInfo_Click()
  3. ' Pulls information out of SQL view.
  4. Dim db As Database
  5. Dim rs As Recordset
  6. Dim tmpUser As String
  7.  
  8.   Set db = CurrentDb
  9.   ' criteria can only return 1 record as only one record can be loaded into the form.
  10.   Set rs = db.OpenRecordset("SELECT * FROM dbo_vwBookingInfo WHERE [Fieldname]=" & yourcriteria)
  11.  
  12.   Me!SalesManager = rs!SM
  13.   Me!ProjectManager = rs!PM
  14.  
  15.   rs.Close
  16.   Set rs = Nothing
  17.   Set db = nothing
  18.  
  19. End Sub
  20.  
  21.  

I have written an Access database that our department uses to store and analyze information on particular jobs. Because our company runs on an enormous SQL DB, I thought it would save some time if the basic job details (name, location, salesperson, etc) which is already stored in the SQL DB could be loaded onto one of my forms at the click of a button. The form needs this information (and more that has to be manually entered) to be complete. I wrote an SQL view and linked it into my Access DB. My idea is to be able to, when entering new jobs, type in a job number on my form, and then at the click of a button, populate several other fields from information already stored in the view. However, I'm getting run-time error 424 "object not found" - even though all relevant fields have been included on my SELECT statement in my form the appropriate INNER JOIN has been done. Here's how I'm doing it:

Expand|Select|Wrap|Line Numbers
  1. Private Sub LoadInfo_Click()
  2. ' Pulls information out of SQL view.
  3. Me!SalesManager = dbo_vwBookingInfo.SM
  4. Me!ProjectManager = dbo_vwBookingInfo.PM
Access does recognize the view as I've successfully written a query with it. Does anybody know how to accomplish what I'm trying to do? Not all jobs in the SQL view should go into my Access table, which is why I'm trying to have it only load information one job at a time on command...
Sep 7 '06 #3
Selesti
10
Thanks so much!

Last question - if I wanted to add a MsgBox and end the process if that particular job number does not exist in the view (if they typed it in incorrectly, for example) how would I go about doing that?

Again, very much appreciated. :)
Sep 8 '06 #4
MMcCarthy
14,534 Expert Mod 8TB
Set rs = db.OpenRecordset("SELECT * FROM dbo_vwBookingInfo WHERE [Fieldname]=" & yourcriteria)

If rs.RecordCount <> 0 Then

'Your code here

Else
Msgbox "This job cannot be found", vbOkOnly
End If


Thanks so much!

Last question - if I wanted to add a MsgBox and end the process if that particular job number does not exist in the view (if they typed it in incorrectly, for example) how would I go about doing that?

Again, very much appreciated. :)
Sep 9 '06 #5
Selesti
10
Rawr...

Now I'm getting a "Too few parameters. Expected 1." run-time error on this line

Expand|Select|Wrap|Line Numbers
  1. Set rs = db.OpenRecordset("SELECT * FROM dbo_vwBookingInfo WHERE [BookingNumber]=" & Me!JobNumber)
  2.  
:( I tried adding

& ";"

at the end, and to no avail. Same error.
Sep 11 '06 #6
miffe
7
You usually just add the SQL in between the () in an OpenRecordset, altough if it is not working, try the next:

Dim rs As RecordSet
Set rs = db.OpenRecordSet("SELECT * FROM dbo_vwBookingInfo WHERE [BookingNumber]=" & Me!JobNumber, dbOpenDynaset)

Note that there are various properties available for OpenRecordSet, I suggest you examine these properties when using VB/Access or SQL access, it can save you time and question-asking.

Greetings,
Miffe
Sep 11 '06 #7
Selesti
10
I have also tried explicitly defining Forms!PreJCEntry!JobNumber ...instead of Me!JobNumber ... still no good.
Sep 11 '06 #8
Selesti
10
I tried this as well, and it did not work. I have searched through various websites and forums as well as the helpfiles and cannot seem to find the answer. I have double checked and rewritten the code as well... dbo_vwBookingInfo exists, BookingNumber is the name of a field in that table, and JobNumber is a legitimate control on my form, but still the same run-time "Too few parameters" error.

You usually just add the SQL in between the () in an OpenRecordset, altough if it is not working, try the next:

Dim rs As RecordSet
Set rs = db.OpenRecordSet("SELECT * FROM dbo_vwBookingInfo WHERE [BookingNumber]=" & Me!JobNumber, dbOpenDynaset)

Note that there are various properties available for OpenRecordSet, I suggest you examine these properties when using VB/Access or SQL access, it can save you time and question-asking.

Greetings,
Miffe
Sep 11 '06 #9
Selesti
10
My next test was to use

Set rs = db.OpenRecordset("SELECT * FROM dbo_vwBookingInfo WHERE BookingNumber=AMPASPUB00001")

Where AMPASPUB00001 was a booking number in the table... STILL same error!

I tried copying the contents of dbo_vwBookingInfo into a new table, and using that table, and still no go as well.

Rawr, any help would be appreciated. I hope I'm not missing something obvious, but it is very frustrating.
Sep 11 '06 #10
MMcCarthy
14,534 Expert Mod 8TB
If BookingNumber is text and not a number field use:

Set rs = db.OpenRecordset("SELECT * FROM dbo_vwBookingInfo WHERE [BookingNumber]='" & Me.JobNumber & "'")


My next test was to use

Set rs = db.OpenRecordset("SELECT * FROM dbo_vwBookingInfo WHERE BookingNumber=AMPASPUB00001")

Where AMPASPUB00001 was a booking number in the table... STILL same error!

I tried copying the contents of dbo_vwBookingInfo into a new table, and using that table, and still no go as well.

Rawr, any help would be appreciated. I hope I'm not missing something obvious, but it is very frustrating.
Sep 11 '06 #11
Selesti
10
If BookingNumber is text and not a number field use:

Set rs = db.OpenRecordset("SELECT * FROM dbo_vwBookingInfo WHERE [BookingNumber]='" & Me.JobNumber & "'")
You are my shining hero. I had tried extra quotation marks, but didn't realize there needed to be apostrophes. Thank you so much.
Sep 11 '06 #12
Hello! I want to get to the same place..I think..but am way behind you both in skill. I know I want fields to autopopulate when I select one field. I got this to happen in a query...but would like it to happen in a form. I have tried manipulating both the sample database and your codes into my database but really just don't know much about codes or Visual Basic. I think im am missing something in this whole process....help!
Sep 12 '06 #13
MMcCarthy
14,534 Expert Mod 8TB
Post your question directly to the forum and a number of experts will attempt to answer it.
Sep 12 '06 #14

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

Similar topics

7
by: Bob | last post by:
Currently I am using this statement to translate 3 fields in my db thru Visual Basic. I import the data from one table to another then call the IFF statements and the NewDate to translate the...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
4
by: bill.brennum | last post by:
Hi, This is probably a question with a simple answer, but here goes. I have a form linked to a table. There is a field on that table that while not showing on the form, I would like to...
1
by: John Phelan-Cummings | last post by:
I'm not certain if this made the post. Sorry if it's a repeat: Using a Button to take an autonumber from one form to populate another autonumber field on another form. I have a Mainform "A"...
16
by: agrawal.solutions | last post by:
Hello Friends I am asking a very silly question but i dont find any solution fo this.. I am selectiong a recordset and want to populate a combobox where id would be inviseble and the content...
1
by: dmeyr | last post by:
Hello, I am new to Access and am having difficulty with a Dlookup function. I have a form that I wish to autopopulate 10 fields with values based on two criteria which are also fields on the form....
5
by: joshua.nicholes | last post by:
I have an access database that consists of two tables.A data collection table and a species list table. The data collection table has about 1500 records in it and the species list has about 600....
3
MattFitzgerald
by: MattFitzgerald | last post by:
My Forms & Tables:- Main form is Frm_LE_List (contains Customer Details) Stored in Tbl_LE_List Which contains subform Frm_VOL_References (Contains Orders known as VOL's) Stored in...
5
by: giandeo | last post by:
Hello Experts. Could you find a solution for this problem please! I have the following tables in Access Database Table Name: origin Fields Names: country, countrycode Table Name: make...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.