Building SQL In Active Form With VBA "I Really Fool For SQL,Please Help Me"
Mar 28, 2006
Dear All,
I want to make a multiple criteria query from active form,
but i stuck in SQL ,Please help to solve my syntac. I am very fool in Sql Syntac. the code like bellow:
Option Compare Database
Private Sub cmdSearch_Click()
On Error GoTo cmdSearch_Click_Err
Dim blnQueryExists As Boolean
Dim cat As New ADOX.Catalog
Dim cmd As New ADODB.Command
Dim qry As ADOX.View
Dim varItem As Variant
Dim strUmur As String
Dim strJnsKelamin As String
Dim strPendidikan As String
Dim strPosisi As String
Dim strJnsKelaminCondition As String
Dim strPendidikanCondition As String
Dim strPosisiCondition As String
Dim strSQL As String
'************************************************
' Check for the existence of the stored query
blnQueryExists = False
Set cat.ActiveConnection = CurrentProject.Connection
For Each qry In cat.Views
If qry.Name = "PelamarTemp_Qry" Then
blnQueryExists = True
Exit For
End If
Next qry
' Create the query if it does not already exist
If blnQueryExists = False Then
cmd.CommandText = "SELECT * FROM Pelamar"
cat.Views.Append "PelamarTemp_Qry", cmd
End If
Application.RefreshDatabaseWindow
' Turn off screen updating
DoCmd.Echo False
' Close the query if it is already open
If SysCmd(acSysCmdGetObjectState, acQuery, "PelamarTemp_Qry") = acObjStateOpen Then
DoCmd.Close acQuery, "PelamarTemp_Qry"
End If
'************************************************* *
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
' Build criteria string for Umur
strUmur = Me.Umur.Value
Debug.Print strUmur
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^
' Build criteria string for JnsKelamin
strJnsKelamin = Me.Jns_kelamin.Value
If Len(strJnsKelamin) = 0 Then
strJnsKelamin = "Like '*'"
Else
strJnsKelamin = "IN(" & strJnsKelamin & ")"
End If
Debug.Print strJnsKelamin
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
' Build criteria string for Pendidikan
strPendidikan = Me.Pendidikan.Value
If Len(strPendidikan) = 0 Then
strPendidikan = "Like '*'"
Else
strPendidikan = "IN(" & strPendidikan & ")"
End If
Debug.Print strPendidikan
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
' Build criteria string for Posisi
strPosisi = Me.Posisi.Value
If Len(strPosisi) = 0 Then
strPosisi = "Like '*'"
Else
strPosisi = "IN(" & strPosisi & ")"
End If
Debug.Print strPosisi
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
' Get JenisKelaminCondition
If Me.optAndJnsKelamin.Value = True Then
strJnsKelaminCondition = " AND "
Else
strJnsKelaminCondition = " OR "
End If
Debug.Print strJnsKelaminCondition
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
' Get PendidikanCondition
If Me.optAndPendidikan.Value = True Then
strPendidikanCondition = " AND "
Else
strPendidikanCondition = " OR "
End If
Debug.Print strPendidikanCondition
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
' Get PosisiCondition
If Me.optAndPosisi.Value = True Then
strPosisiCondition = " AND "
Else
strPosisiCondition = " OR "
End If
Debug.Print strPosisiCondition
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
'************************************************* *
' Build SQL statement
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
'SQL Statement from Query builder
'SELECT Pelamar.No_pelamar, Pelamar.Tgl_lamaran, Pelamar.Nama, Year(Now())-Year([Tgl_lahir]) AS Umur, Pelamar.Jns_kelamin, Pendidikan.Ijazah, Posisi.Posisi
'FROM ((Pelamar INNER JOIN Pekerjaan ON Pelamar.No_pelamar = Pekerjaan.No_pelamar) INNER JOIN Pendidikan ON Pelamar.No_pelamar = Pendidikan.No_Pelamar) INNER JOIN Posisi ON Pelamar.No_pelamar = Posisi.No_pelamar
'WHERE (((Pelamar.Jns_kelamin)=[Forms]![SearchByKriteria_frm]![Jns_kelamin])) OR (((Pendidikan.Ijazah)=[Forms]![SearchByKriteria_frm]![Pendidikan])) OR (((Posisi.Posisi)=[Forms]![SearchByKriteria_frm]![Pendidikan])) OR (((Year(Now())-Year([Tgl_lahir]))<=[Forms]![SearchByKriteria_frm]![Umur]));
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^
'Please I am very stupid in sql,how to apply all variabel that i had create and insert in the sql in vba?
'i had try write as bellow but the result is error number 2465 with error description "Microsot Access can't find the field"I" reffered in your expression"
'All i need is how to write SQL in vba like the sql statement in query builder for make a query on form.
' note: all variabel string is all from the form
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^6
strSQL = "SELECT Pelamar.* FROM ((Pelamar INNER JOIN Pekerjaan ON Pelamar.No_pelamar = Pekerjaan.No_pelamar) INNER JOIN Pendidikan ON Pelamar.No_pelamar = Pendidikan.No_Pelamar) INNER JOIN Posisi ON Pelamar.No_pelamar = Posisi.No_pelamar" & _
"WHERE Pelamar.[Jns_kelamin] " & strJnsKelamin & _
strPendidikanCondition & "Pendidikan.[Ijazah] " & strPosisiCondition & _
"Posisi.[Posisi] " & strJnsKelaminCondition & (Year(Now()) - Year([Tgl_lahir])) = " strUmur "";"
Debug.Print strSQL
'************************************************* *
' Apply the SQL statement to the stored query
cat.ActiveConnection = CurrentProject.Connection
Set cmd = cat.Views("PelamarTemp_Qry").Command
cmd.CommandText = strSQL
Set cat.Views("PelamarTemp_Qry").Command = cmd
Set cat = Nothing
' Open the Query
DoCmd.OpenQuery "PelamarTemp_Qry"
' If required the dialog can be closed at this point
' DoCmd.Close acForm, Me.Name
' Restore screen updating
cmdSearch_Click_Exit:
DoCmd.Echo True
Exit Sub
cmdSearch_Click_Err:
MsgBox "An unexpected error hass occurred." _
& vbCrLf & "Procedure: cmdSearch_Click" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description:" & Err.Description _
, vbCritical, "Error"
Resume cmdSearch_Click_Exit
End Sub
Private Sub optAndJnsKelamin_Click()
If Me.optAndJnsKelamin.Value = True Then
Me.optOrJnsKelamin.Value = False
Else
Me.optOrJnsKelamin.Value = True
End If
End Sub
Private Sub optAndPendidikan_Click()
If Me.optAndPendidikan.Value = True Then
Me.optOrPendidikan.Value = False
Else
Me.optOrPendidikan.Value = True
End If
End Sub
Private Sub optAndPosisi_Click()
If Me.optAndPosisi.Value = True Then
Me.optOrPosisi.Value = False
Else
Me.optOrPosisi.Value = True
End If
End Sub
Private Sub optOrJnsKelamin_Click()
If Me.optOrJnsKelamin.Value = True Then
Me.optAndJnsKelamin.Value = False
Else
Me.optAndJnsKelamin.Value = True
End If
End Sub
Private Sub optOrPendidikan_Click()
If Me.optOrPendidikan.Value = True Then
Me.optAndPendidikan.Value = False
Else
Me.optAndPendidikan.Value = True
End If
End Sub
Private Sub optOrPosisi_Click()
If Me.optOrPosisi.Value = True Then
Me.optAndPosisi.Value = False
Else
Me.optAndPosisi.Value = True
End If
End Sub
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^66
Is there any one can correct my syntac?
View Replies
ADVERTISEMENT
Sep 23, 2006
I've been trying to figure this out, but it looks like a no go.
I wanted to give my users the ability to create/modify queries, but NOT create/modify tables.
Does anyone know if this is possible in Access 2002?
Thanks,
Earl
View 2 Replies
View Related
Nov 21, 2005
I am very new at all of this. I am trying to build a filter form to just pick out one item. I am trying to filter out Box Numbers that have been entered into the database to run reports on them. I think that I am missing the correct code to do this. I have tried to create a form off the query by just using the box number and then creating a command button to apply the filter. But this doesn't work. I'm sure that I'm missing a step along the way. Help!
View 2 Replies
View Related
May 31, 2006
Could someone please give me a link or place to find a descreption of all the "More Tools" that you can use when building a form. If I had a list I could pick what is best for my database and explain things better to management...thanks.
View 3 Replies
View Related
Jan 17, 2006
Apologises if the answer is out there somewhere but I have searched to no avail.
I have a number of Forms which the user can jump around from one to the next. All the forms remain open with the form they are currently viewing showing on top. If they make a change on one particular form then when they return to a previously viewed form I want to disable or hide certain certain controls etc.
I know this would be simple to do on the on open command but as previously mentioned as I am not closing the forms after they are viewed, if the user returns to a previously opened form then this is not picked up.
Soooo, basically I'm after something like
When the user moves from one form to another already opened form for the new form to immediately know that it now has the focus and for it to do something along the lines of on open.
Any help will be appreciated
View 8 Replies
View Related
Aug 28, 2006
Hello,
I am using a form to add data and would like to know if is possible to run a code via macro that allows me to print the records that are showing on the screen (I believe called the active records) without have first closed and saved the data.
Thank you.
View 1 Replies
View Related
Jun 23, 2007
hi
I want to set record selector on the form with a record number that I am founding it with this code:
Code:Dim con As ADODB.ConnectionDim cmd As ADODB.CommandDim rst As ADODB.RecordsetSet con = New ADODB.ConnectionSet cmd = New ADODB.CommandSet rst = New ADODB.Recordsetcon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CurrentProject.Path & "hazineh-bime.mdb" & ";Persist Security Info=False" cmd.CommandText = "select * from table1 where salp=" & Val(Trim(Form_mainhazineh.sal.Value)) & " and mahp=" & (Trim(Form_mainhazineh.mah.Value)) & " " Set cmd.ActiveConnection = con rst.Open cmd, , 1, adLockReadOnly If rst.RecordCount = 1 Then ' found the record else end if
I mean I want to change the active record on the form with another one .
how can I do it?
View 3 Replies
View Related
Sep 14, 2012
I have main form with everything on it using tabs and so on and everything is working fine. What I would like to do is this. Since I am jumping frequently between different records and many actions require main form to be reopened or refreshed and when this happens the form goes to random records, I would like for form to go to last record i was on before it was refreshed or opened.
I was thinking using tempVars to set the id but the problem is that I am not sure what can trigger the TempVars since I am jumping between the records and not making any changes to them.
View 3 Replies
View Related
Apr 19, 2007
Need some help on a command which would close any on screen active form. I want to have this Esc key enabled as an autokey to close any screen active form. I know how this autokey thing works but unable to figure out proper code.
thanks
View 6 Replies
View Related
Aug 18, 2015
Field "A" is the drop list to select data like (Yes, No)
Field "B" is inactive but turn it to active when the field "A" is select to "Yes".
View 1 Replies
View Related
Mar 13, 2015
I have a database with a filter on the form which users can apply I know at the bottom of the form it highlights it as filtered but I want something a bit more prominent displayed on the form when a filter is active.
View 5 Replies
View Related
Aug 18, 2014
I have a table with 4 Fields:
Drawing Number
File Link
Attachments
Description
I basically want to have a live preview window of the "File Link" attachment in that current record. I have heard it's best to use a Web Browser Control when doing this, but I cannot get it to display the currently attached pdf file in that field of that record.
Is there a way to edit the link properties of the Web Browser Control so that it dynamically detects. The file location of the attached file in that field? There will only ever be one attachment in that field, as this will be a live pdf view of the drawing to which that drawing number was assigned
View 1 Replies
View Related
Mar 2, 2015
I have a form where the user clicks a button (button 1) to open a modal form, the user then enters data into this form and clicks close.
Then back in the first form I need to find which control had focus before (button 1) was clicked.
Screen.PreviousControl returns a control on the modal form.
I hope its easy like Forms!FirstForm.PreviousControl but I can't seem to find it!
View 2 Replies
View Related
Jan 28, 2014
Essentially I have a form_A with several tab's and then one list control box in each of those tabs. At present if you double click on any item within the control boxes your taken to another form_B with info about that item and when you close that from down again if refreshes form_A.
Is it possible to only refresh the specific list control box that is active instead of refreshing the whole form ?
View 4 Replies
View Related
Jul 31, 2013
I'm trying to tie a from into the results from and AD Query. I'm not that experienced with doing this kind of thing so I may be going about this the wrong way. Anyway here's what I've got so far:
Code:
Private Sub Form_Open(Cancel As Integer)
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = New ADODB.Connection
With cn
.ConnectionString = "Provider=ADSDSOObject;Trusted_Connection=yes;"
[Code] .....
When stepping through the code, it all goes well until it hits the line that actually connects it to the form "Set Me.Recordset = rs".
When this line is executed, Access crashes and attempts to restart.
PS: Access 2010, Win7 64bit
View 5 Replies
View Related
Jun 14, 2013
I would like to update a txtbox with a simple int count of the number of times a mouse is left-clicked while on an active form. How do I do this?
View 5 Replies
View Related
Jan 2, 2006
Hi, I am desparate!! I work for a high school and need a database to record student community service hours over their high school careers. I have been trying to do this on and off since the summer. I promised I would have it done by the end of winter break but........I may have to go back to a spreadsheet if I can't get this right. That has proved to be a royal pain in the past few years.
I have set up tables but getting the relationships right is a problem. I dont know if it is possible but I would like to set it up so that I have a student form with 4 subforms for each school year. Each subform (4 tabs) will have the title of the school year (i.e. 05-06). We don't allow them to get credit if they go over on their hours for a given year. In other words, they can't do all their service hours in say, freshman year. They have to do 10 as freshmen, 20 as sophomores, 30 as juniors and 40 as seniors.
Each year I can refresh from the school db (for new students, etc.) but I can't go in and create a report in that program as we don't have the capability.
I am trying to teach myself but every tutorial I come across doesn't give reasons why you do something so I can figure out how to convert that info to help me. I have been researching this for a long time and looking at sample dbs.
Can anyone help? I am going to try to attach a copy of the mess I have made so far, Hopefully
Thanks in advance.
View 6 Replies
View Related
Oct 17, 2006
Hello all,
I need some assitant in building a simple check reconciliation database. I was wondering if anyone can guide me in doing so? Or if anyone has some kind of sample i can use that would be great. thanks
View 4 Replies
View Related
Nov 7, 2006
Hello,
I want to build a agenda, like the one shown in the image below.
I have no idea how to start.
Any help would be greatly appreciated.
Thanks in advance
http://www.wega.nl/images/scherm/agenda.gif
View 5 Replies
View Related
May 8, 2006
I have made an Access database containing contract names and addresses. I need to have each of these contracts linked to another table which shows the allocated engineer for the particular contract. I used a "one 2 many" relationship using a field "LinkID" to do this. Each engineer has a unique "LinkID" in the engineer table but the engineer changes each day. The engineer for each particular day comes via an excel spreadsheet. The way it works at the moment is that the engineer name has to be manually changed each day, and this takes time. I have tried importing a linked excel table which looked like it would work, but was not able to use the linked table in the same "one 2 many" related table format as above.
What i need to do, is find a way for the fields containing the engineer name to be automatically updated somehow, possible by reading the values from the excel sheet.
Anybody know of any way for this to be done?
Thanks, and hope this makes sense got its got me baffled.
*** Edit **** Thinking about it, i could have an imported execl sheet which contains all of the engineers for each day. Would there be a way using a macro, to update the data in the engineers table that is related to the contracts, by overwriting it with the data from the linked spreadsheet?
View 4 Replies
View Related
Jun 20, 2005
I have at least 15000 records and all of them should be corrected if there is existing duplicates with same EMBG and different name. More precisely if there are 2 persons with same EBMG lets say 123456789 but one with name Naim Arifi and other one Naum Arifi then query should present to me Naim and Naum. Example
1. Naim Arifi 123456789
2 Elton John 123456452
3. Naum Arifi 123456789
4. Naim Arifi 123456789
* *
* *
* *
* *
722. Naim Arifski 321546798
* *
* *
So the record 1 3 and 4 and 722 should be highlited because they have same EMBG and I need to find where is the mistake manualy (correcting from Naum to Naim). In this case row number 4 is mistake instead of Naim is Naum. I need to correct it manualy.
Could someone provide me some help
View 4 Replies
View Related
Jun 21, 2005
I'm running a 9 parameter query, where values for the criteria are drawn from a form. I'm confused as to how I build an "if, then" string so that if one of the parameters is left blank, the program will skip it and go to the next.
Any suggestions?
View 8 Replies
View Related
Nov 8, 2004
Ok, I currently have a query built with requests. What I want to do is initially set each request with a priority.
Then when one request is closed the query will take the requests with priority 2 and change it to priority 1, change the request with priority 3 to priority 2 and so on. Also any new requests that are entered I want to be able to set their priority as well. Can anyone help me with this, or is it even possible?
Any help would be greatly appreciated!
View 4 Replies
View Related
Mar 11, 2005
I need to create an sql statement where the having statement can grow depending on how many conditions I pass to it. There maybe 10 conditions or there may only be 1 condition statement. I guess I'm really looking more for proper structure. Should I use "if" statements or is there a way to structure a variable with "OR" statements in it and use that.
conditionStr = "Rental"
autoSeg = -221205415
strSQL = "SELECT tblSubSegments.fkSegment, tblSubSegments.fkType, " & _
"Sum([tblSubSegments]![Quantity]*[tblSubSegments]![Units]) AS Expr1 " & _
"FROM tblSubSegments " & _
"GROUP BY tblSubSegments.fkSegment, tblSubSegments.fkType " & _
"HAVING (((tblSubSegments.fkSegment)= " & autoSeg & ") AND " & _
"((tblSubSegments.fkType)= '" & conditionStr & "'))"
rst.Open strSQL, gblEstDataCnn, adOpenKeyset, adLockPessimistic
With rst
count = .RecordCount
End With
This works fine and returns a record count of 1 however if I change the conditionStr to:
conditionStr = " '& PMTravel & 'OR' & Rental &' "
This of course does not work. Thank you for any help.
View 2 Replies
View Related
Feb 23, 2006
Hi,
I am hoping this is the right place to find some help.
I have just started access (2003) and i am trying to build a list.
Let me explain.
I have a table that contains
cartcode, description, price
and another containing
manufacturer, model
I then created a new table that links these tables together so that if i click the little + next to a cartcode i can see a list of models linked with that cartridge.
What i want to do though is create a query/table/anything that will create me a list of cartridges and then in the second column a list, hopefully seperated by a comma or all the printers models related to that cartridge.
I hope that makes sence.
So my question is how do i create the list which included related printers?
Thank you
View 4 Replies
View Related
Mar 17, 2006
Hi all, i'm new here and I have a query about a database i'm building with which any help would be appreciated.
I'm designing a form and I need to be able to have an option to save to a temporary table for retrieval by users so that they can continue their saved record at a later time. Does anyone have any ideas on the best way to do this as I am relatively inexperienced and I have to have the whole database complete before Wednesday:confused:
View 2 Replies
View Related