Why am I getting a run time error 2185 on a form and not on another? - vba

I'm kinda new to MS Access and I'm sort of learning while coding, so forgive me if my question is a bit weird.
I have created a form based on a table, and in the form I have a text box that the user would type something and it should filter the table and show the results based on what the user typed. There are two forms with pretty much the same code on them (named Rec and Cx). In one of them (Rec) the above description works just fine, but the other (Cx) don't and I get a run-time error 2185. Let me show you some code:
Private Sub strConsRecDesc_KeyUp(KeyCode As Integer, Shift As Integer)
FiltroRec = ""
FilterTextDesc = ""
If Len("" & Me.strConsRecDesc.Text) > 0 Then
intLenDesc = Len(Me.strConsRecDesc.Text)
RequeryForm
strConsRecDesc.SetFocus
Me.FilterOn = True
If intLenDesc > Len(Me.strConsRecDesc.Text) Then
Me.strConsRecDesc = Me.strConsRecDesc & " "
Else
Me.strConsRecDesc = FilterTextDesc
End If
strConsRecDesc.SelStart = intLenDesc
Else
RequeryForm
strConsRecDesc.SetFocus
End If
End Sub
I heard it is good practice to lable variables and fields based on data type, so here int stands for integer, str for strings and Desc refers to the Description field.
Based on what is typed in the field strConsRecDesc I filter the table using the RequeryForm in there, that basically checks all the fields in the form that the user can write into. Let me show you the part for the description field:
strConsRecDesc.SetFocus
If Len(strConsRecDesc.Value) > 0 Then
FilterTextDesc = Me!strConsRecDesc.Value
If Len(FiltroRec) > 0 Then
FiltroRec = FiltroRec & " And "
End If
FiltroRec = FiltroRec & "[recDescricao] LIKE '*" & FilterTextDesc & "*'"
End If
In this form (Rec), I can write, i.e. this is a test and no record is shown, because there is no record with this is a test written in it, and that is correct. If I type something that matches the criteria it works just fine.
However, in the other form (the Cx one), I have the following code for KeyUp:
Private Sub strConsCxDesc_KeyUp(KeyCode As Integer, Shift As Integer)
FiltroCx = ""
FilterTextDesc = ""
If Len("" & Me.strConsCxDesc.Text) > 0 Then
intLenDesc = Len(Me.strConsCxDesc.Text)
RequeryForm
strConsCxDesc.SetFocus
Me.FilterOn = True
If intLenDesc > Len(Me.strConsCxDesc.Text) Then
Me.strConsCxDesc = Me.strConsCxDesc & " "
Else
Me.strConsCxDesc = FilterTextDesc
End If
strConsCxDesc.SelStart = intLenDesc
Else
RequeryForm
strConsCxDesc.SetFocus
End If
End Sub
And the equivalent RequeryForm for the Cx is:
strConsCxDesc.SetFocus
If Len(strConsCxDesc.Value) > 0 Then
FilterTextDesc = Me!strConsCxDesc.Value
If Len(FiltroCx) > 0 Then
FiltroCx = FiltroCx & " And "
End If
FiltroCx = FiltroCx & "[cxDescricao] LIKE '*" & FilterTextDesc & "*'"
End If
But in the Cx one if I type this is a test in the strConsCxDesc textbox I get a run-time error 2185.
I understand that with just this bit of code it is kinda hard to grasp what I'm trying to do, but I really don't know why I'm getting this error if the code is the same.
I appreciate any help, and I'm sorry for my bad english, it's not my mother language.
Thanks in advance.

Related

Access: Saving then Loading (and selecting) SelectedItems from ListBoxes

I have a form with multiple listboxes with the MultiSelect property enabled (and a single option group). For row source, each listbox reads two columns (sysID, sysName) from a table (Systems) filtered by a third column (sysType) which corresponds to the type of systems managed in that listbox (e.g. row source is SELECT Systems.sysID, Systems.sysName FROM Systems WHERE Systems.sysType=3 ORDER BY Systems.sysName; for one of them).
I have a save button that executes the following to store a CfgID to the CfgSys table with each sysID (Systems.sysID = CfgSys.sysID) for later recall. It works like this (varSys is an array of Ints):
Save_Config:
i = 0
For Each ctl In frm.Controls
If ctl.ControlType = acListBox Then
For Each varItm In ctl.ItemsSelected
varSys(i) = ctl.ItemData(varItm)
i = i + 1
Next varItm
ElseIf ctl.ControlType = acOptionGroup Then
varSys(i) = ctl.Value
i = i + 1
End If
Next ctl
For i = LBound(varSys) To UBound(varSys)
If (Not IsNull(varSys(i))) And (varSys(i) <> 0) Then
strSQLIns = "INSERT INTO CfgSys (CfgID, SysID) VALUES (" & varCfgID & "," & varSys(i) & ");"
DoCmd.RunSQL (strSQLIns)
End If
Next
Some preamble and wrap-up omitted there for brevity. That part works great, for CfgID 1 I have rows in CfgSys corresponding to each of the 20 or so entries spread among the various listboxes.
I have a load button I would like to read those rows and select the various entries previously stored for each listbox. That part is driving me nuts. So far I have:
Load_Config:
strSQL = "SELECT CfgSys.CfgID, CfgSys.SysID, Systems.sysType, SysTypes.sysTypeName FROM " & _
"(SysTypes RIGHT JOIN Systems ON SysTypes.[SysType] = Systems.[SysType]) " & _
"RIGHT JOIN CfgSys ON Systems.[sysID] = CfgSys.[SysID] WHERE CfgSys.[CfgID] =" & varCfgID & ";"
Set rs = db.OpenRecordset(strSQL)
With rs
If Not .BOF And Not .EOF Then
.MoveLast
.MoveFirst
While (Not .EOF)
If rs!sysTypeName = "Electrical" Then
strCtl = "optElectrical"
frm.Controls(strCtl).Value = rs!sysID
Debug.Print strCtl & ": " & rs!sysID
Else
strCtl = "lst" & rs!sysTypeName
frm.Controls(strCtl).Selected(rs!sysID) = True
Debug.Print strCtl & ": " & rs!sysID
End If
.MoveNext
Wend
End If
End With
Response = MsgBox("Configuration loaded.", vbOKOnly Or vbInformation, "Load Successful")
But I can't figure out how to translate the SysID's/.itemData values from the Save part of the form into indexed line/row #'s for the .Selected collection of the listboxes, so right now it just selects one item in the longest of the listboxes, purely because it has a bunch of rows. Google hasn't helped me nor has Microsoft's reference on ListBox.ItemsSelected. The Debug.Print statement in there successfully prints the name of each listbox control and the sysID corresponding to the row I want to set, but I've been stuck at this last bit for 2 days - anyone able to lend some insight? Is this even possible?
Per June7 above needed to iterate through .ItemData and match that way. Can't seem to mark their comment as the answer, but with their guidance ended up with this:
Else
strCtl = "lst" & rs!sysTypeName
For i = 0 To frm.Controls(strCtl).ListCount - 1
If CStr(rs!sysID) = frm.Controls(strCtl).ItemData(i) Then
frm.Controls(strCtl).Selected(i) = True
End If
Next i
Since ItemData returns strings the CStr was needed to get it to match up.

vb.net position cursor one space greater than text box length

I have a TextBox and it contains this text "File Was Created"
I would like to place the cursor one space over from the end of this text in the TextBox
I am trying to NOT say Simple Enough Task BUT I have wasted 2 hours with no solution
YES I know if I change the text to this "File Was Created " it will work NOT a solution
Here is the code mess I have tried
Dim L As Integer
L = tbMessage.Text.Length
L += 1
'tbMessage.Text = CStr(L)
'tbHaveTwo.Text = frmOne.vR
'Me.ActiveControl = tbMessage
'tbMessage.SelectionStart = tbMessage.Text.Length
tbMessage.SelectionStart = L
tbMessage.Select()<br/>
Here is Two updated ways to solve this issue Jimi way less code
tbMessage.Text = "File Was Created"
'This Code involves more code
'Dim str As String
'str = Mid(tbMessage.Text, tbMessage.Text.Length)
'If str <> " " Then
' tbMessage.Text = tbMessage.Text & " "
'End If
'Answer from Jimi Works Great
tbMessage.AppendText(ChrW(32))
tbMessage.SelectionStart = tbMessage.Text.Length
tbMessage.Select()
So you don't end up with a ton of spaces on the end of your message?
tbMessage.AppendText(If(tbMessage.Text.EndsWith(" "), "", " "))
tbMessage.SelectionStart = tbMessage.TextLength
tbMessage.Focus()

add another lookup to statement

I have this simple block of code on a form that looks up whether or not a part number exists before the order can be entered. It displays either a green check mark if it does or a red X if it doesn't. I would like to add a lookup to make sure the part number is not obsolete (true/false) as well. but I'm not sure what I need to do.
'Here is the code
Private Sub txtPN1_LostFocus()
If Not IsNull(txtPN1) Then
If DCount("[PartNum]", "[PartsFinished]", "[PartNum] = '" & txtPN1 & "'") > 0 Then
pic1.Picture = "\\TORQSERVER\Torq\Shawn\check.png"
Else:
pic1.Picture = "\\TORQSERVER\Torq\Shawn\cross.jpg"
End If
End If
End Sub
That could be:
If DCount("*", "[PartsFinished]", "[PartNum] = '" & txtPN1 & "' And [Obsolete] = False") > 0 Then

how to check if dataset contains specific value in VB.net

I have a dataset that contains multiple values. I want to take those rows from that dataset that contains "the specific value" and firstly I want to display those in a MessageBox.
Furtheron, I try to view them in a datagridview called ErrorsDgV.
I already searched this topic and found a good function, but unfortunately, all I get from the MessageBox is an empty box.
ErrorsDgV.DataSource = Srchdataset.Tables("blubb")
LineLabel.Text = "Lines: " &
Srchdataset.Tables("blubb").Rows.Count.ToString
ErrorsDgV.Sort(ErrorsDgV.Columns(1), System.ComponentModel.ListSortDirection.Ascending)
ErrorsDgV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
ErrorsDgV.Columns(1).DefaultCellStyle.Format = "dd/MM/yyyy HH:mm:ss.fff"
Dim answer As String = ""
Dim SearchRows() As Data.DataRow
SearchRows = Srchdataset.Tables("blubb").Select("Data = 'the specific value'")
answer = ""
For k As Integer = 0 To SearchRows.Length - 1
If answer = "" Then
answer = SearchRows(k).Item("Data")
Else
answer = answer & vbNewLine & SearchRows(k).Item("Data")
End If
Next
MsgBox(" " & answer)
I debugged also and got to know that SearchRows is empty, even if the specific value is inlcuded in that DataSet.

excel vba ping list of computers

I am working on a project. My goal is, to ping all of the computers from an excel list, but can't figure out why it isn't working. I am quite new at this programming language, and I am sure that I miss out something, because I get the error message: Object required
so here is my code
the main:
Sub pingall_Click()
Dim c As Range
c = Target.Name
For Each c In Range("A1:N50")
If (Left(c, 1) = "C" Or Left(c, 1) = "T") And IsNumeric(Right(c, 6)) And Len(c) = 7 Then
c = sPing(c)
If c = "timeout" Then
MsgBox "timeout"
ElseIf c < 16 And c > -1 Then
MsgBox "ok"
ElseIf c > 15 And c < 51 Then
MsgBox "not ok"
ElseIf c > 50 And c < 4000 Then
MsgBox "big delay"
Else
MsgBox "error"
End If
End If
Next c
End Sub
The function:
Public Function sPing(sHost) As String
Dim oPing As Object, oRetStatus As Object
Set oPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
("select * from Win32_PingStatus where address = '" & sHost & "'")
For Each oRetStatus In oPing
If IsNull(oRetStatus.StatusCode) Or oRetStatus.StatusCode <> 0 Then
sPing = "timeout" 'oRetStatus.StatusCode
Else
sPing = sPing & vbTab & oRetStatus.ResponseTime & Chr(10)
End If
Next
End Function
I can get the result if I write sPing(""), but I want it to get the name of pc-s that are in the list.
This is just a test version of the script, I am testing it with one pc for now, that is why I use "MsgBox".
Thank you
The 2nd line inside the Sub pingall_Click() subroutine is the one throwing the Object Required error. i.e. the following line.
c = Target.Name
If you comment it out or delete it, it works. (I tried it.)
Also, you should not be assigning the return value from the function sPing back to c.
Because doing so will overwrite the name of the Server / IP address you have in the cell, since the forloop is looping over 1 cell at a time using the c variable.
So instead, assign it back to a new string variable, and then do whatever you want with it.