Search button for a database in vb.net for listview - database

The code below is not searching, keeps showing error. It is supposed to search for records using a number.
Private Sub btn_search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_search.Click
Me.Sales_fileBindingSource.Filter = "Receipt Number LIKE '" & "%'" & txt_search.Text & "%'"
End Sub
The error being shown is: Syntax error: Missing operand after 'Number' operator.
Click here to see error screenshot

It would make it easier to see what's going on if the code used an interpolated string, so it might end up like:
Me.Sales_fileBindingSource.Filter = $"[Receipt Number] LIKE '%{txt_search.Text}%'"
It is necessary to put the column name in square brackets as it has a space in it - there are examples of the syntax at DataView RowFilter Syntax [C#] (the syntax for the filter is independent of the language).
If you are using an older version of Visual Basic, before interpolated strings were introduced (the year 2015), you'll need something like:
Me.Sales_fileBindingSource.Filter = String.Format("[Receipt Number] LIKE '%{0}%'", txt_search.Text)
which, while a bit longer, still makes it easier to see what is happening than using lots of " & x & "'-type code.

Finally got it! All i had to do was use an under score to join the two words Receipt and Number to one like this "Receipt_Number" Also made other changes like using String.Format. Check code below.
Me.Sales_fileBindingSource.Filter = String.Format("(Convert(Receipt_Number, 'System.String') LIKE '%{0}%')", txt_search.Text)

Related

Syntax check fails on array as parameter to sub

I have two related problems. It seems like I don't have a library attached. I have searched and cannot find what I am missing. This is a call to a sub from a function that is called(referenced) in an Excel cell.
The syntax checker fails when I put an array in as a parameter like this which is taken from this VB Page
Sub EmptyValid210917 _
(RefData As String, RefRow As Integer, PhraseArray As String(,), _
DayIndex As Integer, EmptyCol As Boolean, ValidCol As Boolean)
The second related issue is the syntax check fails when I try to dim an array like this:
Dim ColOffset(,) As Integer
It will accept this:
Dim ColOffset(2, 2) As Integer
But does not accept loading as below which is taken from this VB Guide How To
Dim ColOffset(2, 2) As Integer = {{1,2},{3,4}}
Perhaps this is also related. When I call the max function. I get: Compile Error - Method or data member not found
FirstValidDay = Math.Max(1, 2)
Thank you for your help.

How Can I Filter Filtered Data in DataGridView

I'm Having a Problem in filtering Data in Data Grid View, My code works if I use 1 search filter but if I use 2 search filter it's not, first I need to filter it by Courts then the other one is by venue or location
Please give me any idea on how can I make this code works, thank you
My Code:
Private Sub Searchbtn_Click(sender As Object, e As EventArgs) Handles Searchbtn.Click
'Search Code
If Me.Court_AgencyComboBox.Text = "" Then
MessageBox.Show("No Item to be Search", "IBP Legal Aid Case Management System - Legal Aid Case File - Search", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
If Not String.IsNullOrEmpty(Me.Court_AgencyComboBox.Text) Then
Me.TblLegalAidCaseFileBindingSource.Filter = String.Format("[Court/Agency] like '%{0}%'", Me.Court_AgencyComboBox.Text)
ElseIf Not String.IsNullOrEmpty(Me.Court_AgencyComboBox.Text) And Not String.IsNullOrEmpty(Me.VenueComboBox.Text) Then
Me.TblLegalAidCaseFileBindingSource.Filter = String.Format("[Court/Agency] like '%{0}%' and Venue like '%{0}%'", Me.Court_AgencyComboBox.Text, Me.VenueComboBox.Text)
Else
RefreshData()
Return
End If
End Sub
The way if-else statements work is that it will execute the first one that meets the condition. In your 1-filter case, it will fulfill this condition If Not String.IsNullOrEmpty(Me.Court_AgencyComboBox.Text) and thus execute that line. However, in your 2-filter case, it will check the first condition If Not String.IsNullOrEmpty(Me.Court_AgencyComboBox.Text) and execute that line too, since the condition is actually fulfilled.
An easy way to solve your problem directly is to swap the conditions. Starting with the condition that is the hardest to fulfill. Which will look like this (I nested your conditions to make it cleaner):
(Code before...)
If Not String.IsNullOrEmpty(Me.Court_AgencyComboBox.Text) Then
If Not String.IsNullOrEmpty(Me.VenueComboBox.Text) Then
Me.TblLegalAidCaseFileBindingSource.Filter = String.Format("[Court/Agency] like '%{0}%' and Venue like '%{1}%'", Me.Court_AgencyComboBox.Text, Me.VenueComboBox.Text)
Else
Me.TblLegalAidCaseFileBindingSource.Filter = String.Format("[Court/Agency] like '%{0}%'", Me.Court_AgencyComboBox.Text)
Else (Code after...)

Visual basic 6 issue with combobox

I have created a forum in vb6 and made connection with a database in access,everything went perfect.
my problem is in my form there is 2 combobox one to select Number and other to get me other numbers (watch the video to understand )
anyway the first combo is working and the second combo is working too but after selecting different number from the first combo i don't get anything in the second combo.anyway i know i just miss something in the code something very stupid
i have uploaded a video so you can see my problem, thanks in advance.
Private Sub Form_Load()
liaison
Do Until rspatient.EOF
Me.npa.AddItem rspatient.Fields(0)
rspatient.MoveNext
Loop
End Sub
Private Sub npa_Click()
rspatient.MoveFirst
Dim cr As String
cr = "npation ='" & npa & "'"
rspatient.Find cr
nom = rspatient.Fields(1)
prenom = rspatient.Fields(3)
rshospita.MoveFirst
nh.Clear
While rshospita.EOF = False
If UCase(rshospita.Fields(14)) = UCase(npa) Then
nh.AddItem rshospita.Fields(0)
End If
rshospita.MoveNext
Wend
End Sub
video for more detail :
https://www.youtube.com/watch?v=Tidm18_tvp0
The simplest possible reason for your problem is that you don't have any hospital records associated with your second patient. Check that first.
However, your code is also a bit convoluted. A simpler way to do what you want is to use Filter instead of Find. Filter restricts your recordset to only those records which match the filter, so you can simply iterate the filtered recordset the same way you do the whole one (rspatient). Something like this:
Private Sub npa_Click()
With rshospita
.Filter = ".Fields(14) = '" & npa & "'"
.MoveFirst
nh.Clear
Do Unitl .EOF
nh.AddItem .Fields(0)
.MoveNext
End With
End Sub

Some files have 'XMLNS' listed, others just 'XSI'. How do I set an alias for the xpath query that will work with either?

Some files I need to process have this (called the "haves"):
<ApolloDataSet xmlns="http://irisoft.com/ApolloDataSet1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
Other files in the same group have this (called the "have-nots"):
<ApolloDataSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
I can set the default namespace for xpath with the below:
.setProperty "SelectionNamespaces", "xmlns:a='http://irisoft.com/ApolloDataSet1.xsd'"
That works for the haves, like a:/Element, but not the have-nots, since the xpath doesn't have the a: alias.
I tried removing the xmlns attribute before processing, in the hopes that I could just use an unaliased path for both, like /Element, but that only worked for the have-nots (the haves just returned nothing).
So is there a way to process both using the same alias, or no alias? I'm trying to either use the same alias for every file, regardless of "xmlns" being listed, or use no alias for either.
Well I resigned to using the workaround I was trying to avoid. But here is an option for this situation until I find a better one. (And forgot to mention - using VBA for this.)
After the domDocument is loaded, I use the getAttribute method to check if the "xmlns" attribute value is null. If it is I form the xpath string with no alias by means of a vbnullstring, which means the xpath query works with the have-not files (i.e. those files with no "xmlns" listed). If it is not null, I use the "xmlns" value to build a string for the setProperty method setting the "SelectionNamespaces" secondary property.
If IsNull(xPOG.DocumentElement.getAttribute("xmlns")) Then
strAlias = vbNullString
Else
strAlias = "a:"
xPOG.setProperty "SelectionNamespaces", _
"xmlns:" & Mid(strAlias, 1, 1) & "='" & xPOG.DocumentElement.getAttribute("xmlns") & "'"
End If
And the resulting processing takes on a rather ugly form of string building that I hoped to avoid.
It sets an IXMLDOMNode to the nodes returned from a selectnodes method using the xPath query with the strAlias variable.
Set xProducts = xPOG.SelectNodes("/" & strAlias & "ApolloDataSet/" _
& strAlias & "Products/" _
& strAlias & "Product/" _
& strAlias & "PR_UPC")
It isn't that it is bad, it just seems to lack elegance & remind me of how little I know sometimes. Which leads me to believe I'm missing some concept that could correct this. But it works, so I'll follow up if I find a better. I did find a kb article that touched on some of the concepts if it helps anyone. I'm using 6.0 though, so good conceptual explanation, just not exactly what I needed.
PRB: MSXML 4.0 Sets the XML Namespace Attribute to an Empty Value for Child Nodes

MS Access - open a form taking a field value from a previous form

I have a form in an MS Access database which lists all the landowners consulted with for a new electricity line. At the end of each row is a button which opens another form, showing the details of all consultation, offers made etc.
I am trying to use vb in MS Access to take the contactID and automatically put it in a field in the details form, so that landowner's consultation details will pop up automatically. I am not a vb programmer at all (I have a comp sci degree mostly in Java and I'm currently working as a GIS analyst but it's a small company so I've been asked to get an Access database working).
I want to say
[detailsForm]![contactID] = [landownerlist]![ID]
in a way that vb and access will be happy with. Then I can see if I'm on the right track and if it will actually work! What I have above does not actually work. It won't compile.
From Kaliana
If you wish to open a form to a new record and to set the ID there, you can use Openargs, an argument of Openform:
DoCmd.OpenForm "FormName",,,,acFormAdd,,Me.ID
The opened form would also need some code:
If Me.Openargs<>vbNullstring Then
Me.Id = Me.Openargs
End If
It is also possible to find:
Forms!LandownersList.Recordset.FindFirst "ID=" & Me.ID
or fill in a value:
Forms!LandownersList!Id = Me.ID
on the form being opened from the calling form.
You may want to look into the code that is behind these buttons. If you are using a docmd.openform you can set the 4th Setting to a where clause on openning the next form.
DoCmd.OpenForm "OpenFormName", acNormal, , "[contactID] = " _
& [detailsForm]![contactID] , acFormEdit, acWindowNormal
This assumes contact ID is numeric and doesn't require any quotes.
Using open args is the generally accepted solution, as alluded to by others. This just falls under the category of "For you edification":) One of the problems with using open args is that unless you are careful with your comments it's easy to forget what they were supposed to mean. Were you passing more than one? Which is which? How did I do it here? How did I do it there etc. For my own money, I standardized to this (below) so I can always pass more than one argument without fear, and when I review my code a year from now, I can still see what's what without a huge hassle:
Option Explicit
'Example use: DoCmd.OpenForm "Example", OpenArgs:="Some Filter|True"
Public Enum eForm1Args
eFilter = 0
eIsSpecial = 1
End Enum
Private m_strArgs() As String
Public Property Get Args(ByVal eForm1Args As eForm1Args) As String
Args = m_strArgs(eForm1Args)
End Property
Private Sub Form_Open(Cancel As Integer)
m_strArgs = Split(Nz(Me.OpenArgs, vbNullString), "|")
If LenB(Me.Args(eFilter)) Then Me.Filter = Me.Args(eFilter)
End Sub
Private Sub Command1_Click()
If LCase$(Me.Args(eIsSpecial)) = "true" Then
'Do something special
End If
End Sub
As previously posted OpenArgs is great for this. One trick I have learned is that it is easy to pass in multiple parameters if required as a delimited string (comma for example), the target form can then access these values using the Split() function thus:
StringArrayVariable()= Split(me.OpenArgs,",")
Me.textbox= StringArrayVariable(0)
Me.textbox1= StringArrayVariable(1)
etc.
This is air code so check out the helpfile for Split().
It is also possible to pass objects in OpenArgs as well, it requires some manual memory pointer manipulation and I don't have the code to hand but I'm sure a Google search will find some examples. This technique can cause some random crashes though. Be Warned!

Resources