Combo box selection as text in string - ms-access

I have a Form in Access which uses a selection from a combo box list to drive the creation of a report.
The combo box selection is evaluated as an ID. I need to get it as a text. I've found other similar issues but cannot get it to work. Can anyone walk me through it?
The closest thread I found was http://www.access-programmers.co.uk/...ad.php?t=58062, but not sure what "joining to the lookup table" entails.
In the code below, txtExpr1001, txtIndustry, and txtSkill are the combo boxes.
The code:
Private Sub cmdSubmit_Click()
Dim sWhereClause As String
sWhereClause = "Expr1001 = '" & txtExpr1001.Value & "' AND MonthsCon >= " & txtMonth1.Value & " AND IndustryName = '" & txtIndustryName.Value & "' AND MonthsNonCon >= " & txtMonth2.Value & " AND Skill = '" & txtSkill.Value & "' AND MonthsSkills >= " & txtMonth3.Value & ""
Call DoCmd.OpenReport("IndustryCon + IndustryNonCon + Skills", acViewPreview, , sWhereClause)
End Sub
My code does not trigger an error. The report opens but it's blank.

If you want to get the value of a combo box (which I'm not sure which of your .values are the combo box if any are) then you can just write something like this:
Private Sub cmdSubmit_Click()
Dim sWhereClause As String
'Me.Combo_BoxName.Column(0) is going to be how you call the combo box
'I put it right at the front of the string and chose ID as a random name as well
sWhereClause = "ID = '" Me.Combo_BoxName.Column(0) & "' AND Expr1001 = '" & txtExpr1001.Value & "' AND MonthsCon >= " & txtMonth1.Value & " AND IndustryName = '" & txtIndustryName.Value & "' AND MonthsNonCon >= " & txtMonth2.Value & " AND Skill = '" & txtSkill.Value & "' AND MonthsSkills >= " & txtMonth3.Value & ""
Call DoCmd.OpenReport("IndustryCon + IndustryNonCon + Skills", acViewPreview, , sWhereClause)
End Sub

Related

How to exclude columns in Access VBA to maintain data

I have a Access form with a bunch of buttons and some text boxes.
The text boxes automatically fill to the table. Thanks to someone else on here I figured out how to sync the button press to the fill the table. Now I need to ensure that the rest of information in the table stays when I press the button and the "Yes" get recorded. Currently all the other column of information doesnt record to the sheet but the button press does. How do I pass by those columns and keep their data and still record the various button presses that need to happen?
Thanks in advance!!
Below is the current button code that records on the table.
Private Sub CWTButton_Click()
Dim Sql As String
Sql = "INSERT INTO [Raw Data] (CWT) VALUES ('Yes');"
CurrentDb.Execute Sql
End Sub`
New Code
Private Sub CWTButton_Click()
Dim Sql As String
Sql = "INSERT INTO [RawData] ([Date], Staff, Species, Location, Length, Fish_ID, Comment, CWT) VALUES ('" & Me!Date.Value & "', '" & Me!Staff.Value & "', '" & Me!Species.Value & "', '" & Me!Location.Value & "', '" & Me!Length.Value & "', '" & Me!Fish_ID.Value & "','" & Me!Comment.Value & "','Yes');"
CurrentDb.Execute Sql
End Sub
New Code
Sql = "INSERT INTO [RawData] ([Date], Staff, Species, Location, Length, Fish_ID, Comment, CWT) VALUES (#" & Format(Me!Date.Value, "yyyy\/mm\/dd" & "#', '" & Me!Staff.Value & "', '" & Me!Species.Value & "', '" & Me!Location.Value & "', " & Me!Length.Value & ", " & Me!Fish_ID.Value & ",'" & Me!Comment.Value & "','Yes');"

Subform column and data change at runtime not refreshing on Form

I have Main Form in which There are Two Comobobx.
1. Type of Work (C_Art: Name of Combobox)
Repratur
Maintenance
Optimierung
2. Year (C_Year: Name of Combobox)
2018
2019
On the Main Form There is Bar chart which works perfectly and a subform with a datasheet view.
Overall it looks like .
My Problem is when I select the Year 2019 the Subform view with new header 2019-Jan 2019-Feb ......2019-Dec didn't update and the row give me error as #Name?
The Graph is generating correctly without any Problem.
I am changing the query at runtime as below.
Dim count_Status As String
Dim G_Stoerung As String
'Change the query for Graph
count_Status = "TRANSFORM Count(History_query.[LFD_NR]) AS CountOfLFD_NR " & _
" SELECT History_query.[Anlage] " & _
" FROM History_query " & _
" WHERE History_query.[Anlage] <>Null AND History_query.[WAS]= '" & Me.C_Art & "' AND History_query.[Year]= '" & Me.C_Year & "' " & _
" GROUP BY History_query.[Anlage] " & _
" PIVOT History_query.[Status] "
CurrentDb.QueryDefs("Count_Status_Anlage").sql = count_Status
'Updating the Graph with a new Data
Me.Graph9.Requery
Me!Graph9.ChartTitle.Text = Me.C_Art
'Chnage the query of subform
G_Stoerung = "TRANSFORM Count([History_query].WAS) AS CountOfWAS " & _
" SELECT [History_query].Status " & _
" FROM History_query " & _
" WHERE ((([History_query].WAS)= '" & Me.C_Art & "' ) AND [History_query].Status IN ('Offen','beobachten','In bearbeitung','erledigt') ) " & _
" GROUP BY Status " & _
" Order by (IIf([Status]='Offen',1,IIf([Status]='beobachten',2,IIf([Status]='In bearbeitung',3,IIf([Status]='erledigt',4))))) " & _
" PIVOT Format([Datum],'yyyy-mmm') IN ('" & Me.C_Year & "-Jan','" & Me.C_Year & "-Feb','" & Me.C_Year & "-Mar','" & Me.C_Year & "-Apr','" & Me.C_Year & "-May','" & Me.C_Year & "-Jun','" & Me.C_Year & "-Jul','" & Me.C_Year & "-Aug','" & Me.C_Year & "-Sep','" & Me.C_Year & "-Oct','" & Me.C_Year & "-Nov','" & Me.C_Year & "-Dec') "
CurrentDb.QueryDefs("G_Stoerung_query").sql = G_Stoerung
'Updating the subform with new Data
Me.G_Stoerung_Sub.Form.RecordSource = G_Stoerung
Me.G_Stoerung_Sub.Form.Requery
The #NAME? error would be because textboxes are no longer bound to fields that exist. Make the field names generic and apply filter parameter for year. Or set the subform container SourceObject property to query object instead of form
Edit
G_Stoerung_Sub.SourceObject = ""
G_Stoerung_Sub.SourceObject = "Query.G_Stoerung_query"
updating the source object gives desired result.

How do I have a message box appear when something is entered in text box and button is pressed?

Is it possible to make a pop-up message box to appear when something is written in the textbox? It doesn't matter to me what the text box actually has I just want a message to appear when a button is pressed and there is text in the textbox.
The following is what I currently have:
Private Sub cmdReg_Click()
CurrentDb.Execute "INSERT INTO qryShowAll(Student_FirstName, Student_LastName, Parent_FirstName, Parent_LastName, AddressLine, City, State, Zip, PhoneNumber) VALUES ('" & Me.txtStudentFirst & " ',' " & Me.txtStudentLast & "','" & Me.txtParentFirst & " ',' " & Me.txtParentLast & "','" & Me.txtAddress & " ',' " & Me.txtCity & "',' " & Me.txtState & "','" & Me.txtZip & "','" & Me.txtNumber & "')"
If (txtStudentFirst,txtStudentLast,txtParentFirst,txtParentLast,txtAddress ,txtCity,txtState,txtZip,txtNumber) = " "
MsgBox "The student was successfully registered.", vbOKOnly, "Student Registered!"
Else
MsgBox "Student was not registered, please complete the form.", vbOKOnly, ""
End If
One way is to use + sign for concatenating the textboxes. This will result in Null if even one is Null. This approach requires setting text fields in table to not allow empty string.
If IsNull(txtStudentFirst + txtStudentLast + txtParentFirst + txtParentLast + txtAddress + txtCity + txtState + txtZip + txtNumber) Then
MsgBox "Student was not registered, please complete the form.", vbOKOnly
Else
MsgBox "Student was successfully registered.", vbOKOnly, ""
End If
Alternatively, set fields as required in table and let Access nag the user to complete. Can also use ValidationRule and ValidationText properties to help ensure data is entered.

MS Access Tag property - how its identifying update and insert operation?

I trying to create Add,update,Delete operation on MS Access form.I found this code on internet where Insert and update is happening on the same button. I am not getting what is exactly happening in below line and how it's identifying it is for update or insert.
Not getting following line : = Me.txtid.Tag & "" = ""
Please find below code which works perfect as per requirement.
'When we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtid.Tag & "" = "" Then
' this is for insert new
' add data in table
CurrentDb.Execute "insert into student(stdid,stdname,gender,phone,address)" & _
" values(" & Me.txtid & ",' " & Me.txtname & " ',' " & Me.cmbgender & " ','" & _
Me.txtphone & "', '" & Me.txtaddress & "')"
'refresh data in list on form
subform_student.Form.Requery
Else
CurrentDb.Execute "UPDATE student " & _
" set stdid = " & Me.txtid & _
", stdname = '" & Me.txtname & "' " & _
", gender = '" & Me.cmbgender & " ' " & _
", phone = ' " & Me.txtphone & " ' " & _
", address = ' " & Me.txtphone & " ' " & _
" WHERE stdid = " & Me.txtid.Tag
End If
The .Tag property is a general-purpose string property of every form and control object in VBA/VB6. It is provided as a place for developers to "put stuff" to support the operation of their applications.
The original code from which you copied your sample must have written a value to Me.txtid.Tag when the record was loaded (e.g., perhaps in the form's Current event) to indicate whether the record is an existing record or a new record (empty="new", non-empty="existing"). The line
If Me.txtid.Tag & "" = "" Then
simply checks to see if the .Tag property is empty, and then performs the INSERT or UPDATE accordingly.
BTW, re:
below code which works perfect as per requirement
No, it doesn't. Try adding a record where [stdname] is Tam O'Shanter and see for yourself. You should ditch the dynamic SQL and use one of
a bound form (as Gustav suggests),
a parameterized query, or
a recordset update.
Forget/remove all this code and bind the form to table Student to make this all happen automatically.
If a bound form is not familiar to you, browse for a tutorial for "Beginning with Microsoft Access" or the like.

Access, For Each loop to add multiple txtfields into table for each row in another table

My question might be confusing, I'm looking for a way to solve issue with 'maintenance' system written in Access.
There is a form with data, and I need to take txtFields values, listBox values and add them as sql row for each phone number that is stored in another table.
For example, dbShift contain few phone numbers (from 8 to 15) and they are switched each 8 hours.
When worker submit form with 'machine failure', maintenance office receives it thru access front-end.
This information need to be added in to another table as well, lets call it dbSMS, where we will store: nrMaszyny, nazwaMaszyny, Zglaszajacy, Przyczyna, Obszar, Telefon.
Now for each phone number stored in dbShift (numbers are switched by the form created for this table) this message needs to be duplicated.
It's all to automate process and send automatically SMS from the modem we have in the company, so maintenance employees will be informed right away about the issue, not only thru their workshop front end.
Everything seems to be done, but I'm stuck with the way of implementing that 'For Each' way to collect neccessary information and add them to table, where modem can feed the data to pass it thru SMS.
Maybe I'm wrong, maybe there is a better way for it than 'For Each'?
Any ideas?
dbShift:
ID, name, phoneNumber
dbSms:
nrMaszyny, nazwaMaszyny, Zglaszajacy, Przyczyna, Obszar, Telefon (data from FORM where worker sends machine failure form)
form from where You collect data:
Code of this form:
Option Compare Database
Private Sub buttonZamknijFormrularz_Click()
DoCmd.Close DoCmd.OpenForm "MainLoginForm"
Forms!MainLoginForm!txtPassword.Value = ""
End Sub
Private Sub cmdAdd_Click()
If IsNull(Me.listPrzyczyna) Or Me.listPrzyczyna = "" Then
MsgBox "Wybierz przyczynę awarii!", vbOKOnly, "Wymagane dane"
Me.listPrzyczyna.SetFocus
Else
Dim strSQL As String
strSQL = "INSERT INTO dbAwarieOtwarte (nrMaszyny, nazwaMaszyny, Zglaszajacy, dataZgloszenia, dataZakonczenia,
godzinaZgloszenia, Przyczyna, Obszar, Telefon, Komentarz) VALUES ('" &
Me!txtNrMaszyny & "', '" & Me!txtNazwa & "', '" & Me!txtZglaszajacy &
"', '" & Me!txtData & "', '" & Me!txtData & "', '" & Me!txtGodzina &
"', '" & Me!listPrzyczyna & "," & Me!txtNazwa & "," & Me!txtNrMaszyny
& "," & Me!txtObszar & "', '" & Me!txtObszar & "', '" & Me!txtTel &
"', '" & Me!txtKomentarz & "');"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
DoCmd.Close ' DoCmd.OpenReport "repAwariaOtwarta", acViewPreview, , "ID=" & DMax("ID", "dbAwarieOtwarte")
End If
End Sub
Private Sub Polecenie35_Click()
DoCmd.Close DoCmd.OpenForm "formMaszynyObszar1"
End Sub
I suggest modifying your code to do something like the following... You said there is another table with phone numbers, so I will loop thru that table. Not sure if you want to use DAO or ADO, so I stuck with DAO.
Private Sub cmdAdd_Click()
Dim dbs as DAO.Database
Dim rsPhone as DAO.Recordset
If IsNull(Me.listPrzyczyna) Or Me.listPrzyczyna = "" Then
MsgBox "Wybierz przyczynę awarii!", vbOKOnly, "Wymagane dane"
Me.listPrzyczyna.SetFocus
Else
Dim strSQL As String
Set dbs = CurrentDB
Set rsPhone = dbs.OpenRecordset("<YourPhoneTable>")
Do While Not rsPhone.EOF
'<<< MAKE YOUR CHANGES TO GRAB THE PHONE NUMBER if needed for the following SQL
' 3/10/16 - changed SQL to add phonenumber to the insert.
strSQL = "INSERT INTO dbAwarieOtwarte (nrMaszyny, nazwaMaszyny, Zglaszajacy, dataZgloszenia, dataZakonczenia,
godzinaZgloszenia, Przyczyna, Obszar, Telefon, Komentarz, PhoneNumber) VALUES ('" &
Me!txtNrMaszyny & "', '" & Me!txtNazwa & "', '" & Me!txtZglaszajacy &
"', '" & Me!txtData & "', '" & Me!txtData & "', '" & Me!txtGodzina &
"', '" & Me!listPrzyczyna & "," & Me!txtNazwa & "," & Me!txtNrMaszyny
& "," & Me!txtObszar & "', '" & Me!txtObszar & "', '" & Me!txtTel &
"', '" & Me!txtKomentarz & "', '" & rsPhone!PhoneNumber & "');"
'DoCmd.SetWarnings False
dbs.Execute strSQL
'DoCmd.SetWarnings True
rsPhone.moveNext
Loop
rsPhone.Close
set rsPhone = Nothing
dbs.close
set dbs = Nothing
DoCmd.Close ' DoCmd.OpenReport "repAwariaOtwarta", acViewPreview, , "ID=" & DMax("ID", "dbAwarieOtwarte")
End If
End Sub