Access VB does not seem to read from the database - ms-access

Set rst = CurrentDb.OpenRecordset("SELECT role" & _
" FROM EMP " & _
" WHERE id =" & Chr(34) & id & Chr(34))
If rst.Fields(role) = "admin" Then
cmdConnecterPrivilege.Visible = True
Else
cmdConnecterPrivilege.Visible = False
End If
What am I doing wrong? Thanks! I want to see if this employee is an admin

You need the right syntax, either of these:
If rst.Fields("role") = "admin"
If rst("role") = "admin"
If rst!role = "admin"
But you can use one code line for this simple task:
cmdConnecterPrivilege.Visible = (Nz(DLookup("role", "EMP", "ID = '" & id & "'")) = "admin")

Related

2010 Access report, partial match from inputted search criteria

I have a report that uses the following SQL query:
SELECT AccountPerformanceAllHistory.AccountNumber,
AccountMaster.AccountName AS Name, AccountCurrentModel.Model,
AccountPerformanceAllHistory.MarketValue,
AccountPerformanceAllHistory.Cash, ModelDetailAllHistory.Risk,
AccountPerformanceAllHistory.QTD, AccountPerformanceAllHistory.YTD,
AccountPerformanceAllHistory.[1Yr], AccountPerformanceAllHistory.[3Yr],
AccountMaster.AccountAdvisor AS Advisor,
AccountPerformanceAllHistory.PerformanceDate,
AccountPerformanceAllHistory.Ticker
FROM ModelDetailAllHistory INNER JOIN ((AccountMaster INNER JOIN
AccountPerformanceAllHistory ON AccountMaster.[AccountNumber] =
AccountPerformanceAllHistory.[AccountNumber]) INNER JOIN
AccountCurrentModel ON AccountMaster.[AccountNumber] = AccountCurrentModel.
[AccountNumber]) ON ModelDetailAllHistory.[ModelName] =
AccountCurrentModel.Model
GROUP BY AccountPerformanceAllHistory.AccountNumber,
AccountMaster.AccountName, AccountCurrentModel.Model,
AccountPerformanceAllHistory.MarketValue,
AccountPerformanceAllHistory.Cash, ModelDetailAllHistory.Risk,
AccountPerformanceAllHistory.QTD, AccountPerformanceAllHistory.YTD,
AccountPerformanceAllHistory.[1Yr], AccountPerformanceAllHistory.[3Yr],
AccountMaster.AccountAdvisor, AccountPerformanceAllHistory.PerformanceDate,
AccountPerformanceAllHistory.Ticker
ORDER BY ModelDetailAllHistory.Risk, AccountPerformanceAllHistory.[1Yr]
DESC;
The report is run from a button-click:
Private Sub Command3_Click()
Dim StrWhichMonth As String
Dim StrWhichClient As String
Dim CYear, CMonth As Variant
Dim StrSearch As String
StrWhichMonth = InputBox("Enter YYMM you want to report:")
CYear = "20" & Mid(StrWhichMonth, 1, 2)
CMonth = Mid(StrWhichMonth, 3, 2)
StrWhichMonth = DateSerial(CYear, CMonth + 1, 0)
StrWhichClient = InputBox("Enter Client name ('*' for all):")
StrSearch = "AccountPerformanceAllHistory.PerformanceDate = #" & StrWhichMonth _
& "# AND AccountPerformanceAllHistory.Ticker = " & Chr(34) & "1" & Chr(34) & "" _
& " AND AccountMaster.AccountName Like " & Chr(34) & StrWhichClient & Chr(34)
StrWhichMonth = "AccountPerformanceAllHistory.PerformanceDate = #" & StrWhichMonth _
& "# AND AccountPerformanceAllHistory.Ticker = " & Chr(34) & "1" & Chr(34) & ""
DoCmd.SetWarnings False
' DoCmd.OpenReport "RPTAccountPerformanceAllHistorySummary", acViewReport, , StrWhichMonth
DoCmd.OpenReport "RPTAccountPerformanceAllHistorySummary", acViewReport, , StrSearch
DoCmd.SetWarnings True
End Sub
As you might can tell, I added the search for AccountName in the button click code. The old code works fine.
The new code, with the StrSearch string does not work. When the query is run, it prompts for "AccountMaster.AccountName" after the two InputBox prompts. I know this means something is wrong with the StrSearch, but I don't know what is wrong. I've searched around the web, but could not find a solution.
Any help is appreciated.
TIA
The field list in your source query includes this one ...
AccountMaster.AccountName AS Name
Since you aliased the field, the query result set does not include a field named AccountMaster.AccountName, so Access assumes it must be a parameter and asks you to supply a value for it. Use the alias instead instead of the original field name.
Change this ...
& " AND AccountMaster.AccountName Like " & Chr(34) & StrWhichClient & Chr(34)
to this ...
& " AND [Name] Like " & Chr(34) & StrWhichClient & Chr(34)

Record with blank field not visible after using filter

II MAKE MAKING A ACCESS DATABASE.I have a form with a subform and few combo box to use as a filter the data. when there is a blank field in the table it is not shown my coding for the filter is
Function searchcriteria()
Dim device, vlan As String
Dim task, strciteria As String
If IsNull(Me.cbodevice) Then
device = "[DEVICE NAME] like '*'"
Else
device = "[DEVICE NAME]= '" & Me.cbodevice & "'"
End If
If IsNull(Me.cbovlan) Then
vlan = "[VLAN ID] like '*'"
Else
vlan = "[VLAN ID]= '" & Me.cbovlan & "'"
End If
strcriteria = device & "And" & vlan
task = "select * from L2PORTDETAILS where " & strcriteria
Me.L2PORTDETAILS_subform.Form.RecordSource = task
Me.L2PORTDETAILS_subform.Form.Requery
End Function]
It may be simpler just to set the Filter property:
Function SearchCriteria()
' Preset Me!L2PORTDETAILS_subform.Form.RecordSource to:
' "select * from L2PORTDETAILS"
Dim Filter As String
If Not IsNull(Me!cbodevice.Value) Then
Filter = "[DEVICE NAME]= '" & Me!cbodevice.Value & "'"
End If
If Not IsNull(Me!cbovlan.Value) Then
If Filter <> "" Then
Filter = Filter & " And "
End If
Filter = Filter & "[VLAN ID] = '" & Me!cbovlan.Value & "'"
End If
With Me!L2PORTDETAILS_subform.Form
.Filter = Filter
.FilterOn = (Filter <> "")
End With
End Function

MS Access VB: How do I update a table with a parametrized query ONLY with textboxes that are not empty?

I have some VBA code that will update a table based on a form. It works great, except that I want the table to only update wherever the user fill in info. If that textbox is instead blank, do no update that field in the table. Below is the working update code. The line that starts with If query.Parameters("P1")... is my attempt at trying to make an If statement that will find which textbox values are Null and then ignore those, but I don't know if that's even going in the right direction.
Private Sub Command133_Click()
'Update row for downtime
Dim dbsCurrent As Database
Set dbsCurrent = CurrentDb
', suffix, production_date, reason, downtime_minutes, comment ,'" & CInt(Me.Text118) & "','" & CDate(Me.Text126) & "','" & Me.Text121 & "','" & CDbl(Me.Text123) & "','" & Me.Text128 & "'
'dbsCurrent.Execute " INSERT INTO tbl_Downtime (production_date) SELECT #" & Me.Text126 & "# FROM tbl_Downtime As t WHERE t.ID = " & Me.Text135 & ";"
'dbsCurrent.Execute "UPDATE tbl_Downtime SET job = '" & Me.Text116 & "', suffix = '" & Me.Text118 & "', production_date = #" & Me.Text126 & "#, reason = '" & Me.Text121 & "', downtime_minutes = " & Me.Text123 & ", comment = '" & Me.Text128 & "', shift = '" & Me.Text144 & "' WHERE t.ID = " & Me.Text135 & ";"
Dim query As QueryDef
Dim sql As String
For Each query In CurrentDb.QueryDefs
If query.Name = "UpdateDowntime" Then
Exit For
End If
Next query
If query Is Nothing Then
sql = "parameters " & _
"P1 text, P2 text, P3 Date, P4 Text, P5 Number, P6 Text, P7 Text, P8 Number;" & _
"UPDATE [tbl_Downtime] " & _
"SET job = [P1], suffix = [P2], production_date = [P3], reason = [P4], downtime_minutes = [P5], comment = [P6], shift = [P7] " & _
"WHERE[tbl_Downtime].id = [P8]"
'"(job, suffix, production_date, reason, downtime_minutes, comment, shift) " & _
'" VALUES ([P1], [P2], [P3], [P4], [P5], [P6], [P7]) WHERE[tbl_Downtime].id = [P8]"
Set query = CurrentDb.CreateQueryDef("UpdateDowntime", sql)
End If
query.Parameters("P1").Value = Me.Text116
query.Parameters("P2").Value = Me.Text118
query.Parameters("P3").Value = Me.Text126
query.Parameters("P4").Value = Me.Text121
query.Parameters("P5").Value = Me.Text123
query.Parameters("P6").Value = Me.Text128
query.Parameters("P7").Value = Me.Text144
query.Parameters("P8").Value = Me.Text135
If query.Parameters("P1").Value = "" Then Set query.Parameters("P1").Value = job End If ' WHERE [tbl_Downtime].id = [P8] END
query.Execute
An empty string is not the same as a null value in a database, so you need to change the parameter value to nothing if you want it to equate to null (or default value if one exists).
You could either conditionally set the parameter value like this:
If textbox1.text <> "" then
query.Parameters("P1").Value = textbox1.text
End if
or you could write a function to set the parameter to nothing if the textbox is empty:
Function NothingIfEmpty(value As String)
If value = "" Then
NothingIfEmpty = Nothing
Else
NothingIfEmpty = value
End If
End Function
and use it like this:
query.Parameters("P1").Value = NothingIfEmpty(textbox1.text)
query.Parameters("P2").Value = NothingIfEmpty(textbox2.text)
You can do it this way:
"SET job = Nz([P1], job), suffix = Nz([P2], suffix), production_date = Nz([P3], production_date), reason = Nz([P4], reason), downtime_minutes = Nz([P5], downtime_minutes), comment = Nz([P6], comment), shift = Nz([P7], shift) " & _
"WHERE [tbl_Downtime].id = Nz([P8], -[id])"

Can not find the Field name

strSQL = " SELECT W.wrhID, " & _
" W.wrhName AS WName " & _
" FROM tblWarehouse AS W " & _
" WHERE W.wrhID IN ( " & Forms.frmStockControl.Form.txtwrhIDs & " )"
Set rst = CurrentDb.OpenRecordset(strSQL)
Do Until rst.EOF
Dim strlbl$, strlblV$
For i = 1 To rst.Fields.count
strlbl = "Me.lblWarehouse" & i
strlblV = "Me.lblWarehouse" & i
Me.Controls(strlbl).Caption = rst!WName
Me.Controls(strlblV).visible = True
Next
rst.MoveNext
Loop
I am getting error msg 2465 - Can not find the Field name
but field Name exists in my form.Pls help.
The correct syntax to addres a form control in VBA is either:
Forms![YourFormName]![YourControlName]
The brackets are only required if the name contains blanks.
or
Forms("YourFormName").Controls("YourControlName")
i changed
strlbl = "Me.lblWarehouse" & i
strlblV = "Me.lblWarehouse" & i
to :
strlbl = "lblWarehouse" & i
strlblV = "lblWarehouse" & i
and is working fine

VBA Error in Access: "The cammand or action 'RecordsGoToNew' isn't available now"

I would like to apologize in advance. The form I am dealing with is very complicated with many moving parts so I am sorry it my explanations aren't clear.
I have a form that contains many subforms. One particular subform, fAdminToolsDetails, has a subform of its own:sfAdminToolsYearly. Depending on the state of the overall form, different queries and tables will populate the forms with information. I designed the states with use of an enum . Depending on which enum value is passed into the sub the state will change and the appropriate record sources, control sources, and so on are updated. However, the previously mentioned form, sfAdminToolsYearly, does not update correctly. Its information still reflects that of the previous state until a user interacts with its parent: fAdminToolsDetails.
I personally this this is fine. However, my client is concerned this might cause some confusion. In order to rectify this issue I attempted to tell the form to go to a new record, just to clear out all the data as well as create a new option or record addition. However, I receive the error in the title for all states except DEPARTMENT.
I have been looking at this all day and can't seem to find the root cause of the error. Please look at my code below and see if you can help. The error is initially thrown on the very last line.
Please let me know if there are any questions about the code. I will be happy to answer them.
Thank You
Private Function changeState(ByVal state As EnumState, ByVal CrrntId As Long)
Dim prevSub As String
Dim level As String
Dim lvlSub As String
Dim prevLvl As String
Dim fYrIndex As Integer
Dim crrntDeptIndex As Integer
Dim crrntDivIndex As Integer
Dim crrntSDivIndex As Integer
Dim crrntSSDivIndex As Integer
Dim crrntSSSDivIndex As Integer
Me![crrntDept].SetFocus
crrntDeptIndex = Me![crrntDept].ListIndex
Me![crrntDiv].SetFocus
crrntDivIndex = Me![crrntDiv].ListIndex
Me![crrntSDiv].SetFocus
crrntSDivIndex = Me![crrntSDiv].ListIndex
Me![crrntSSDiv].SetFocus
crrntSSDivIndex = Me![crrntSSDiv].ListIndex
Me![crrntSSSDiv].SetFocus
crrntSSSDivIndex = Me![crrntSSSDiv].ListIndex
crrntState = state
Dim selectStatement As String
Select Case state
Case DEPARTMENT
level = "Department"
lvlSub = "Dept"
prevLvl = vbNullString
prevSub = vbNullString
Case DIVISION
level = "Division"
lvlSub = "Div"
prevSub = "Dept"
prevLvl = "Department"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![sectionNumber].ControlSource = "DivYearly_SectionNum"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![SectionNumberLabel].Caption = "Division Number"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![tbNumbersPages].Visible = True
If CrrntId = -1 Then
CrrntId = Me!Dept_ID
End If
Case SUBDIVISION
level = "SubDivision"
lvlSub = "SubDiv"
prevSub = "Div"
prevLvl = "Division"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![sectionNumber].ControlSource = "SubDivYearly_ParaNum"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![SectionNumberLabel].Caption = "Subdivision Letter"
If CrrntId = -1 Then
CrrntId = Me!Div_Id
End If
Case SUBSUBDIVISION
level = "SubSubDivision"
lvlSub = "SubSubDiv"
prevSub = "SubDiv"
prevLvl = "SubDivision"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![sectionNumber].ControlSource = "SubSubDivYearly_SubParaNum"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![SectionNumberLabel].Caption = "SubSubdivision Number"
If CrrntId = -1 Then
CrrntId = Me!SubDiv_ID
End If
Case SUBSUBSUBDIVISION
level = "SubSubSubDivision"
lvlSub = "SubSubSubDiv"
prevSub = "SubSubDiv"
prevLvl = "SubSubDivision"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![sectionNumber].ControlSource = "SubSubSubDivYearly_SubSectionNum"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![SectionNumberLabel].Caption = "SubSubSubdivision Letter"
If CrrntId = -1 Then
CrrntId = Me!SubSubDiv_ID
End If
End Select
Me![lvlSub].ControlSource = "='" & lvlSub & "'"
Me.sfListOfLvlData.LinkChildFields = vbNullString
Me.sfListOfLvlData.LinkMasterFields = vbNullString
Me.fAdminToolsDetails.LinkMasterFields = vbNullString
Me.fAdminToolsDetails.LinkChildFields = vbNullString
If state <> DEPARTMENT Then
Me.RecordSource = "SELECT DISTINCT " & prevLvl & "." & prevSub & "_ID FROM " & prevLvl & " WHERE " & prevLvl & "." & prevSub & "_ID = " & CrrntId
Me.sfListOfLvlData.Form.RecordSource = "SELECT DISTINCT " & level & "." & lvlSub & "_ID, " & level & "." & lvlSub & "_Short_Name, " & level & "Yearly." & prevSub & "_ID FROM " & level & " " & _
"INNER JOIN " & level & "Yearly ON " & level & "." & lvlSub & "_ID=" & level & "Yearly." & lvlSub & "_ID" & " ORDER BY " & level & "." & lvlSub & "_Short_Name"
Me.sfListOfLvlData.LinkChildFields = prevSub & "_ID"
Me.sfListOfLvlData.LinkMasterFields = prevSub & "_ID"
Else
Me.sfListOfLvlData.Form.RecordSource = "SELECT " & level & "." & lvlSub & "_ID, " & level & "." & lvlSub & "_Short_Name FROM " & level & " ORDER BY " & lvlSub & "_Short_Name"
Me.sfListOfLvlData.LinkChildFields = vbNullString
Me.RecordSource = level
Me.sfListOfLvlData.LinkMasterFields = vbNullString
End If
Me![Title].ControlSource = "='" & level & "'"
Me.sfListOfLvlData.Form![ShortName].ControlSource = lvlSub & "_Short_Name"
Me![parentId].ControlSource = prevSub & "_ID"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Yearly_Year].SetFocus
If Not state = DIVISION Then
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![tbNumbersPages].Visible = False
End If
If Not state = DEPARTMENT Then
Me.fAdminToolsDetails![COFRSCode].Visible = False
Me.fAdminToolsDetails![functArea].Visible = False
Me.fAdminToolsDetails![chbOperatingBudget].Visible = False
Me.fAdminToolsDetails![chbCapitalBudget].Visible = False
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![sectionNumber].Visible = True
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![tabDirectors].Visible = False
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Parent].Visible = True
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Parent].RowSource = "SELECT " & prevLvl & "." & prevSub & "_Short_Name, " & prevLvl & "." & prevSub & "_ID " & _
"FROM " & prevLvl & " " & _
"ORDER BY " & prevLvl & "." & prevSub & "_Short_Name"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Parent].ControlSource = prevSub & "_ID"
Else
Me.fAdminToolsDetails![COFRSCode].Visible = True
Me.fAdminToolsDetails![functArea].Visible = True
Me.fAdminToolsDetails![chbOperatingBudget].Visible = True
Me.fAdminToolsDetails![chbCapitalBudget].Visible = True
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![tabDirectors].Visible = True
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Parent].Visible = False
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![sectionNumber].Visible = False
End If
Me.mainID.ControlSource = "=[sfListOfLvlData]![" & lvlSub & "_ID]"
Me.fAdminToolsDetails.Form.RecordSource = level
Me.fAdminToolsDetails.LinkMasterFields = "mainID"
Me.fAdminToolsDetails.LinkChildFields = lvlSub & "_ID"
Me.fAdminToolsDetails![Name].ControlSource = lvlSub & "_Name"
Me.fAdminToolsDetails![Short_Name].ControlSource = lvlSub & "_Short_Name"
Me.fAdminToolsDetails![Cd].ControlSource = lvlSub & "_Cd"
Me.fAdminToolsDetails![Active].ControlSource = lvlSub & "_Active"
Me.fAdminToolsDetails![Id].ControlSource = lvlSub & "_ID"
Me.fAdminToolsDetails![lstItemByYear].RowSource = "SELECT " & level & "Yearly." & lvlSub & "Yearly_Id, " & level & "Yearly." & lvlSub & "Yearly_Year" & _
" FROM " & level & "Yearly" & _
" WHERE (((" & level & "Yearly." & lvlSub & "_ID) = [Forms]![testAdminForm]![mainID])) " & _
"ORDER BY " & level & "Yearly." & lvlSub & "Yearly_Year"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly.Form.RecordSource = level & "Yearly"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly.LinkMasterFields = "Yearly_Id"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly.LinkChildFields = lvlSub & "Yearly_ID"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Yearly_Id].ControlSource = lvlSub & "Yearly_ID"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Parent_Id].ControlSource = lvlSub & "_ID"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Yearly_Year].ControlSource = lvlSub & "Yearly_Year"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Yearly_Order].ControlSource = lvlSub & "Yearly_Order"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Yearly_Name].ControlSource = lvlSub & "Yearly_Name"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Yearly_FDesc].ControlSource = lvlSub & "Yearly_FDesc"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Yearly_SDesc].ControlSource = lvlSub & "Yearly_SDesc"
Me![crrntDept].SetFocus
Me.crrntDept = Me.crrntDept.ItemData(crrntDeptIndex)
Me![crrntDiv].SetFocus
Me.crrntDiv = Me.crrntDiv.ItemData(crrntDivIndex)
Me![crrntSDiv].SetFocus
Me.crrntSDiv = Me.crrntSDiv.ItemData(crrntSDivIndex)
Me![crrntSSDiv].SetFocus
Me.crrntSSDiv = Me.crrntSSDiv.ItemData(crrntSSDivIndex)
Me.fAdminToolsDetails.Form.sfAdminToolsYearly.SetFocus
Me.fAdminToolsDetails.Form.sfAdminToolsYearly.Form.AllowAdditions = True
' Me.fAdminToolsDetails.Form.sfAdminToolsYearly.Form!Yearly_Order.SetFocus
DoCmd.RunCommand acCmdRecordsGoToNew
End Function
The solution to my problem was a simple one: and kind of a rookie mistake on my part. I was not setting the focus to the form I was wanting to manipulate
Final code:
Me.fAdminToolsDetails.Form.sfAdminToolsYearly.Form.SetFocus
DoCmd.RunCommand acCmdRecordsGoToNew
Thank You for all your help
it is because you try to add a new record to a form that is shown not from a table but from a select
change record source from "select from table"
to "table"
it is impossible to so insert into a select when using a remote database