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

selection multi criteria access and vba

hey i need ur help please it's my first time working with vba and access and i want to program an access form for multi search i have the type of contract as a combo box if i select type of contrat as "credit IMMO" it displays automatically an other zone of txt,and i need a code that according to the type of contract selected and the other conditions the result of the query have to be inserted in the table destined for this type of contract; i tried this peace of code for the first type of contract but it doesn't work :/ and i'm really sorry for my English :)



Expand|Select|Wrap|Line Numbers
  1. Private Sub btselect_Click()
  2. Dim sql As String
  3. Select Case (Me.cmbrechtype)
  4. Case "crédit IMMO"
  5. sql = "Insert Into [Prêts IMMO Part] Values (Select Num_contrat, Num_personne, Num_instruction, Nom_client, catégorie_client,region, groupe, agence, date_debut_contrat, montant_prêt, objet_prêt, résidence_bien,durée_résiduelle, montant_CRD, Encours, Date_mer0, note_personne, moteur_notation From contrat Where type_contrat = 'crédit immo');"
  6. If (Me.txtdatesup <> "") Then
  7. sql = sql & " AND Date_debut_contrat < #" & Me.txtdatesup & "#"
  8. End If
  9.  
  10. If (Me.txtdateinf <> "") Then
  11. sql = sql & " AND Date_debut_contrat > #" & Me.txtdateinf & "#"
  12. End If
  13.  
  14. If (Me.txtnotesup <> "") Then
  15. sql = sql & " AND Note_personne < #" & Me.txtnotesup & "#"
  16. End If
  17.  
  18. If (Me.txtnoteinf <> "") Then
  19. sql = sql & " AND Note_personne > #" & Me.txtnoteinf & "#"
  20. End If
  21.  
  22. If (Me.txtmtsup <> "") Then
  23. sql = sql & " AND Montant_prêt > #" & Me.txtdatesup & "#"
  24. End If
  25.  
  26. If (Me.txtmtinf <> "") Then
  27. sql = sql & " AND Montant_prêt < #" & Me.txtdateinf & "#"
  28. End If
  29. sql = sql & ";"
  30. DoCmd.RunSQL sql
  31. End Select
  32. End Sub
Jun 10 '15 #1
3 1313
Seth Schrock
2,965 Expert 2GB
Please use CODE tags when posting code per Site Rules.

When you set the INSERT part of the SQL variable, you have a semi-colon at the end of that string and then you add criteria later with all your If-Then statements. The semi-colon marks the end of the query in SQL, so having text after that makes the query fail. Remove that semi-colon and it looks like it should work.
Jun 10 '15 #2
thank you i did what u said but it doesn't work :/ this is what i did

Expand|Select|Wrap|Line Numbers
  1. Private Sub btselect_Click()
  2. Dim sql As String
  3. Select Case (Me.cmbrechtype)
  4. Case "crédit IMMO"
  5. sql = "Insert Into [Prêts IMMO Part] Values (Select Num_contrat, Num_personne, Num_instruction, Nom_client, catégorie_client,region, groupe, agence, date_debut_contrat, montant_prêt, objet_prêt, résidence_bien,durée_résiduelle, montant_CRD, Encours, Date_mer0, note_personne, moteur_notation From contrat );"
  6. sql = sql & " Where type_contrat='crédit IMMO'"
  7.  
  8. If (Me.txtdatesup <> "") Then
  9. sql = sql & " AND Date_debut_contrat < #" & Me.txtdatesup & "#"
  10. End If
  11.  
  12. If (Me.txtdateinf <> "") Then
  13. sql = sql & " AND Date_debut_contrat > #" & Me.txtdateinf & "#"
  14. End If
  15.  
  16. If (Me.txtnotesup <> "") Then
  17. sql = sql & " AND Note_personne < #" & Me.txtnotesup & "#"
  18. End If
  19.  
  20. If (Me.txtnoteinf <> "") Then
  21. sql = sql & " AND Note_personne > #" & Me.txtnoteinf & "#"
  22. End If
  23.  
  24. If (Me.txtmtsup <> "") Then
  25. sql = sql & " AND Montant_prêt > #" & Me.txtdatesup & "#"
  26. End If
  27.  
  28. If (Me.txtmtinf <> "") Then
  29. sql = sql & " AND Montant_prêt < #" & Me.txtdateinf & "#"
  30. End If
  31. sql = sql & ";"
  32. DoCmd.RunSQL sql
  33. sql = sql & " AND Date_debut_contrat < #" & Me.txtdatesup & "#"
  34.  
  35.  
  36. End Select
  37. End Sub
Jun 10 '15 #3
Seth Schrock
2,965 Expert 2GB
You didn't do what I said because the semicolon is still there. You have (broken up for ease of reading):
Expand|Select|Wrap|Line Numbers
  1. sql = "Insert Into [Prêts IMMO Part] 
  2. Values (Select Num_contrat, Num_personne, Num_instruction, 
  3. Nom_client, catégorie_client, region, groupe, agence, 
  4. date_debut_contrat, montant_prêt, objet_prêt, résidence_bien,
  5. durée_résiduelle, montant_CRD, Encours, Date_mer0, 
  6. note_personne, moteur_notation From contrat );"
At the end of this is a semicolon. This needs removed.

I also realized that you don't need the word VALUES when entering from a SELECT query. Also, since you aren't specifying column names, then you need to make sure that you have the proper number of columns being entered into [Prets IMMO Part]. So at this point your query should be
Expand|Select|Wrap|Line Numbers
  1. sql = "Insert Into [Prêts IMMO Part] 
  2. Select Num_contrat, Num_personne, Num_instruction, 
  3. Nom_client, catégorie_client, region, groupe, agence, 
  4. date_debut_contrat, montant_prêt, objet_prêt, résidence_bien,
  5. durée_résiduelle, montant_CRD, Encours, Date_mer0, 
  6. note_personne, moteur_notation From contrat
Jun 10 '15 #4

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

Similar topics

3
by: B Love | last post by:
Hello group, this is a repost that received no responses and has since gotten burried:: I have two databases in a ssl folder on a web site. One is a shopping cart database that facilitates all...
7
by: damjanu | last post by:
Hi All; I need little help. I have a datasheet form. I allow user to do 'filter by selection'. My form contains a column with values. As user changes selections, I want to calculate totals....
5
by: premmehrotra | last post by:
I currently have a multi-user access database which is put on a shared drive L: on a Windows Servers. Entire database is one file premdb.mdb. Users access this database from their laptops....
2
by: Dave Cabbage | last post by:
Hi, At work there is an Access DB which sits on a common server and is accessed by multiple users (up to 9 concurrently). Sometimes this database "falls over", is this because of the number of...
10
by: molen malat | last post by:
i have a query with 4 fields, and a form based on it. i put another 4 textbox to get criteria to filter the query. the query runs normally when all the textbox have a value (not null) but when one or...
0
by: Need Info | last post by:
I know it is possible to have multi-user Access database on a local server but I do not have a server. What is the best way to create a multi-user Access database that can be accessed by any...
2
by: Deb Auer | last post by:
Access 2003 using dlookup to make a button visible. FYI MSPContactUserID(same as Environ("username")) table t41ContactMSP looks like this MSPContactID(PK) MSPTitleID MSPContactUserID 1 ...
2
by: MyDanes | last post by:
update PHYS_COUNT_TAG set COUNT_QTY= (SELECT qty from MC_PART_LOCATION where MC_PART_LOCATION.part_id = PHYS_COUNT_TAG.PART_ID and MC_PART_LOCATION.location_id = PHYS_COUNT_TAG.LOCATION_ID) THE...
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: 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
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.