Using SQL inside a Form

MS Office Access

    Next

  • 1. Lookup in a table
    When using a lookup, Access plugs in the following select statement ... SELECT [Component Lookup].[Component ID], [Component Lookup].[Description] FROM [Component Lookup] ORDER BY [Component Lookup].[Description]; I need to perform the same operation in my code so I can grab some additional fields from the lookup table. I was able to grab these fields using the 'on click' event as follows: If Component_ID.ListIndex < 0 Then Exit Sub Supplier_ID.Value = Component_ID.Column(2, Component_ID.ListIndex) Part_No.Value = Component_ID.Column(3, Component_ID.ListIndex) Unit_Cost.Value = Component_ID.Column(4, Component_ID.ListIndex) With what you're going to suggest for above, will these statements still work. Thanks! -Howard
  • 2. Deleting Duplicate Records in a Form
    hi, i have a form with 4 fields. when trying to add new records in a form i am not notified if i am adding duplicates. is there any way of detecting when a duplicate record is going to be added and how can i notify the user and clear the form before a record has been added. i have set an autonumber that defines the record. i would like the procedure to detect for duplicates after an item has been selected from the last field and the user presses the enter key. thx
  • 3. "Can't add record(s); join key of table "SUBFORMS UNDERLYING TABLE NAME" not in record set" Error message
    I have a subform linked to a main form and populated by a query that uses the Link Master field on the main form as the criteria. The query is based on a separate table than the main form. The query pulls fields from the second table using a current value from the main form as the criteria. Whenever I try to add a new record I get the error message shown in the subject. The tables relationship is a one-to-many where the Main form has the one side and the Subform has the many. There exists some records in the second table which pull up as expected, but I can not add any records. I click on the >* (new record) button and the subform won't allow me to enter data into any of the fields. I searched the MS KB but could not find any help on this issue. I appreciate any insight. -- from Dave the wave~~~~~
  • 4. crystal reports
    how to add crystal reports to access reports
  • 5. Updating a recordet - repost
    I am still having problems with the following: I have 10 fields in a datasheet sub form, if a particular criteria is met 5 of those fields are added to a table (tableB), using the recordset and the Addnew method. If the value of one of the 5 fields changes I would like to have it change in tableB. I have tried using the recordset and the Edit method but it is adding another record to tableB. I've defined the recordset used .edit .fields("Field1") = newValue .fields("field1") = newValue .update however it is updating the incorrect record. Any suggestions?

Using SQL inside a Form

Postby Charles » Sun, 12 Apr 2009 01:39:57 GMT

Hello,

I have a form that has two fields in it (FILE NUMBER and CASE NUMBER) - I 
would like to set up the form so that when the form loads, it queries the 
PERSON table for those two parameters, then retrieves the First and Last 
name from the PERSON table, deletes all the records from another table 
called PERSON_DROPDOWN, and then inserts the First and Last name values into 
that PERSOn_DROPDOWN table. Any ideas? 



Re: Using SQL inside a Form

Postby JP » Sun, 12 Apr 2009 02:23:55 GMT

How do the values get into that form when it loads?  Are you sure it's a 
LOAD event that you want or the CURRENT event as you move from record to 
record?

If the data is already in text boxes on the form, then you can use an event 
such as CURRENT to run some code that does what you want.  For example, you 
could create SQL strings to do what you want and then invoke them in the 
code using docmd.runsql sqlstring.  You could also get fancy and create 
queries with parameters that point back to the form, and then just invoke 
the queries through docmd.openquery queryname.

If you're truly talking about the data being there at load, before any data 
actually appears on the form, then you would pick up the FILE NUMBER and 
CASE NUMBER through code in the LOAD event (e.g., passed as open arguments, 
picked up from a global variable).

Given that you are calling this the PERSON_DROPDOWN, I suspect that you're 
using it as the controlsource for a dropdown (e.g., combobox) on this form. 
If that's the case, you don't have to do any of that and you don't need the 
PERSON_DROPDOWN table.  Instead, you just need to make the controlsource of 
the dropdown control a string that extracts from table PEOPLE based on the 
values of the FILE NUMBER and CASE NUMBER.

For example, if the controls on your form are called txbFILE_NUMBER and 
txbCASE_NUMBER, then the controlsource for the dropdown would be

"select * from PERSON where [FILE NUMBER] = '"  & ME.txbFILE_NUMBER  & "' 
AND [CASE NUMBER] = '" & ME.txbCASE_NUMBER & "'"

NOTE:  I'm assuming that FILE NUMBER and CASE NUMBER are text fields and not 
number fields such as integers.  If they are number fields, you would leave 
out the single apostrophes (').

If you do that, the only people who will show up in the dropdown are those 
in table PERSON who are associated to the FILE NUMBER and CASE NUMBER.  This 
would be much faster than what you are now doing.


-------------
Chaos, panic, & disorder - my work here is done.








Re: Using SQL inside a Form

Postby JP » Sun, 12 Apr 2009 02:35:14 GMT

Oooops

Minor error

If you're setting the control source in the combobox itself, you would not 
have "me."  It would just be


You would have the "me." if you were creating a string in event code.

-------------
Chaos, panic, & disorder - my work here is done.











Re: Using SQL inside a Form

Postby Charles » Sun, 12 Apr 2009 02:35:35 GMT

es, thanks- I am using it as a source for the combobox - the problem is
this - while this form is open, I will have to have another form open that
edits records on the PERSON table - and using the PERSON table as the source
for the combo boxes isnt an option as it's locking the records for editing,
thereby preventing the other form from making changes to the PERSON table.

Hence my problem.

If I could use another source for the comboboxes than the PERSON table (say,
truncatting and populating another table with just the entries I need based
on the FILE NUMBER and CASE NUMBER, then I shouldnt run into this problem.
I'm just stuck on how to do the VB code in the form to truncate, select and
insert the records I need.


"JP" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...



Re: Using SQL inside a Form

Postby JP » Sun, 12 Apr 2009 02:55:24 GMT

The default in Access is optimistic locking, which means that the PERSON 
table is not locked, but rather conflicts in updates are detected.  Since 
you'll only be reading it, it won't be affected by having it open for 
update.

If it is actually locked, is it locked for update or actually locked for 
read?  More often than not, even when a table is locked, it's locked for 
update and not for read.  Check and see if the table PERSON is truly locked 
when you have that form open.   If it is truly locked, you have to ask 
yourself why.  It sounds as if you're in a situation where any one 
individual user is the only user that should be touching that particular 
PERSON/FILE NUMBER/CASE NUMBER at a time.

Be that as it may, to deal with your PERSON_DROPDOWN table,

the delete of PERSON_DROPDOWN would be two lines of VBA code

ssql = "delete from person_dropdown"
docmd.runsql ssql

That deletes all the records from person_dropdown.

Then you would want a basic extract/insert would be two lines of VBA code

ssql = "INSERT INTO PERSON_DROPDOWN (LAST, FIRST) SELECT LAST, FIRST FROM 
PERSON WHERE  [CASE NUMBER] = casenumber AND [FILE NUMBER] = filenumber"
docmd.runsql ssql

Replace the lower case casenumber and filenumber with the variable or 
control name that has the case number and file number.  Don't forget the 
single apostrophes if it's text data.

What you're doing is putting a SQL command in the string (ssql) and then 
using that string as the parameter to the docmd.runsql command.

-------------
Chaos, panic, & disorder - my work here is done.







Similar Threads:

1.appear or add more form inside form , or inserting more textfield inside form

2.Running SQL inside forms

How do you create a recordset inside a form

I have

dim rs as recordset

 strSQL = "SELECT Time_Of_Appointment FROM Appointment WHERE Agent_ID = " & 
Agent_ID.Value

    Set rs = DoCmd.RunSQL(strSQL)

but it errors saying expected function or variable.

I am used to doing this in ASP but cant figure out why in access.

any help would be much appreciated 


3.refer to a form's text box.text inside a SQL clause

On 16 Feb, 15:03, "giannis" < XXXX@XXXXX.COM > wrote:
> How can i refer to a form's textbox.text inside a SQL clause ?
>
> I know how is that in Access* but at VB i receive error.
>
> * SELECT FIELD1 FROM TABLE1
>    WHERE TABLE1.FIELD2=[FORMS]![TEXTBOX].[NAME]


Hi Giannis try this

Dim strwhereclause as string  =textbox1.text

SELECT FIELD1 FROM TABLE1 WHERE TABLE1.FIELD2= " ' " + strwhereclause
+" ' "

OR

SELECT FIELD1 FROM TABLE1 WHERE TABLE1.FIELD2= " ' " + textbox1.text
+" ' "

Thanks

Adam

4.refer to a form's text box.text inside a SQL clause

How can i refer to a form's textbox.text inside a SQL clause ?

I know how is that in Access* but at VB i receive error.

* SELECT FIELD1 FROM TABLE1
   WHERE TABLE1.FIELD2=[FORMS]![TEXTBOX].[NAME] 


5.Scroll inside a form, not between records, using mouse wheel

I have the same database on two different computers. On one the mouse scrolls 
between fields on the same record (on a form) and on the other it scrolls 
between records. I dont want it to scroll between records. How do I change 
the mouse wheel behaviour?  I'm using Access 2003

6. Alternative to using =[Forms]![Form]![Control] as a criteria to filter a query

7. child form inside the page

8. Creating 2nd transaction form inside db ?



Return to MS Office Access

 

Who is online

Users browsing this forum: No registered users and 47 guest