Visual basic 6 issue with combobox - 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

Related

Search button for a database in vb.net for listview

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)

Autofilter Criteria with Array

I have been looking to find a solution and cant find anything online that fully explains what is going on. I looked at some other posts but they all seem to fall a bit short.
When I run this bit of code it works perfectly (as it was recorded).
ActiveSheet.Range("$A$1:$AL$1002").AutoFilter Field:=17, Criteria1:=Array( _
"73578", "78759", "78765"), Operator:=xlFilterValues
But then when I try to make it more robust it fails. I want to change the Criteria1 argument to an already stored array.
I am trying to get the following to work.
ActiveSheet.Range("$A$1:$AL$1002").AutoFilter Field:=17, _
Criteria1:=Array(StoredArray.Values), Operator:=xlFilterValues
I have the array stored and I manipulate it anyway but I have yet to get anything to work. I have also tried to create a string to be exactly like the recorded macro but that does not work either.
Dim StoredArrayString as Variant
StoredArrayString = "73578"", ""78759"", ""78765"
ActiveSheet.Range("$A$1:$AL$1002").AutoFilter Field:=17, _
Criteria1:=StoredArrayString, Operator:=xlFilterValues
Thanks for the help here I have spent lots of time on MSDN trying to research this issue but can't find a solution.
I think your issue is with how you're defining your array. Try this instead.
Dim StoredArrayString As Variant
StoredArrayString = Array("73578", "78759", "78765")
ActiveSheet.Range("$A$1:$AL$1002").AutoFilter Field:=17, _
Criteria1:=StoredArrayString, Operator:=xlFilterValues
Starting with:
and run:
Sub Macro7()
Dim ary(1 To 3) As String
ary(1) = "Alice"
ary(2) = "Boris"
ary(3) = "James"
ActiveSheet.Range("$A$1:$D$22").AutoFilter Field:=3, Criteria1:=ary, Operator:=xlFilterValues
End Sub
will produce:

Long Array VBA Issue

I'm trying to enter a long-array Formula into VBA that is >255 characters. I have followed past suggestions to dim both halves of the formula and merge them later. I am still having errors getting the array to function properly and was hoping someone could help review the code.
Here's the original code that exceeds the character limit I'm trying to get working:
Sub TestMacro()
Range("AZ7").Select
Selection.FormulaArray = _
"=SUM(IF(CONCATENATE(R3C3,[#Route],[#[Assumed Coating Type]],[#Diameter],[#[Year Installed (Coating)]])=CONCATENATE(HCA!R26C[86]:R13642C[86],HCA!R26C[-48]:R13642C[-48],HCA!R26C[87]:R13642C[87],HCA!R26C[-19]:R13642C[-19],HCA!R26C[88]:R13642C[88]),HCA!R26C[-36]:R13642C[-36]))"
End Sub
Here is my latest attempt to split the code in half following past advice: https://www.mrexcel.com/forum/excel-questions/853889-long-array-visual-basic-applications-issue.html
http://dailydoseofexcel.com/archives/2005/01/10/entering-long-array-formulas-in-vba/
Sub LongArrayFormula()
Dim theFormulaPart1 As String
Dim theFormulaPart2 As String
theFormulaPart1 = "=SUM(IF(CONCATENATE(R3C3,[#Route],[#[Assumed Coating Type]],[#Diameter],[#[Year Installed (Coating)]])""X_X_X)"")"
theFormulaPart2 = "=CONCATENATE(HCA!R26C[86]:R13642C[86],HCA!R26C[-48]:R13642C[-48],HCA!R26C[87]:R13642C[87],HCA!R26C[-19]:R13642C[-19],HCA!R26C[88]:R13642C[88]),HCA!R26C[-36]:R13642C[-36]))"
With ActiveSheet.Range("AZ7")
.FormulaArray = theFormulaPart1
.Replace """X_X_X)"")", theFormulaPart2
End With
Any help is appreciated, thanks.
You should keep your truncated formula syntactically correct. Try it like this:
theFormulaPart1 = "=SUM(IF(CONCATENATE(R3C3,[#Route],[#[Assumed Coating Type]],
[#Diameter],[#[Year Installed (Coating)]])=X_X_X,HCA!R26C[-36]:R13642C[-36]))"
' ^^^^^
theFormulaPart2 = "CONCATENATE(HCA!R26C[86]:R13642C[86],HCA!R26C[-48]:R13642C[-48],HCA!R26C[87]:R13642C[87],HCA!R26C[-19]:R13642C[-19],HCA!R26C[88]:R13642C[88])"
With ActiveSheet.Range("AZ7")
.FormulaArray = theFormulaPart1
.Replace "X_X_X", theFormulaPart2
End With
Here I inserted X_X_X (could be anything else) in the place of some "closed expression" in the formula. That keeps the truncated formula correct from a syntax point of view, so the statement .FormulaArray = theFormulaPart1 can accept it. Replacement can then proceed in the second step.
You can also try this(
please don't run it from the VBE, try to run it from sheets environment. Go to Developer-Macros-Your Macro -Run or Run it from a button or shortcut and it will work without problem):
Range("AZ7").Select
Selection.Formula = _
"=SUM(IF(CONCATENATE(R3C3,[#Route],[#[Assumed Coating Type]],[#Diameter],[#[Year Installed (Coating)]])=CONCATENATE(HCA!R26C[86]:R13642C[86],HCA!R26C[-48]:R13642C[-48],HCA!R26C[87]:R13642C[87],HCA!R26C[-19]:R13642C[-19],HCA!R26C[88]:R13642C[88]),HCA!R26C[-36]:R13642C[-36]))"
SendKeys "{F2}"
SendKeys "^+{ENTER}"

VBA Excel 2013: Assign Array Values from Another UserForm

I'm relatively new to VBA. I have a program I'm writing where the user is given the option to change their input from a 2 dimensional array in another user form.
The first user form, UserForm1, allows the user to input the information from text fields and saves it to the respective array row, i, when pressing a Save command button.
When the user presses an OK command button, the user is asked if they want to add another set of data. If they say no, they are asked if they want to change data. If they say yes, then another user form, UserForm2, is opened.
The code for UserForm1 is similar to the code below:
Public MyArray as Variant, i as Integer
Sub Userform_Initialize()
ReDim MyArray(100,4)
End Sub
Sub SaveButton_click()
MyArray(i, 1) = TextField1.Value
MyArray(i, 2) = TextField2.Value
MyArray(i, 3) = TextField3.Value
MyArray(i, 4) = TextField4.Value
End Sub
Sub OKButton_click()
If msgbox("Do you want to add more data?", vbYesNo) = vbNo Then
If msgbox("Do you have corrections to be made?",vbYesNo) = vbYes Then
Load UserForm2
UserForm2.Show
Else: Exit Sub
End If
Else: i = i + 1
Exit Sub
End If
End Sub
In UserForm2, the user chooses the row number, i, from a combo box. When the row number is selected, the array information is automatically populated in text fields from UserForm1.
When the user presses the Save command button, it should pass the information from the text fields and write it to the respective row.
The code for UserForm2 is similar to the code below:
Public j as integer
Sub Userform_Initialize()
For j = 1 to UserForm1.i
ComboBox1.AddItem (j)
Next
End Sub
Sub SaveButton_click()
UserForm1.MyArray(ComboBox1.Value, 1) = TextField1.Value
UserForm1.MyArray(ComboBox1.Value, 2) = TextField2.Value
UserForm1.MyArray(ComboBox1.Value, 3) = TextField3.Value
UserForm1.MyArray(ComboBox1.Value, 4) = TextField4.Value
End Sub
Stepping through the code, the values from MyArray should be properly referenced, and I can see the values initially saved from UserForm1. However, the values are not changing as I step to the next line.
Does anyone have a solution for my problem? Thank you in advance for your help!
I believe I found my solution. I had to declare the array in the module containing the code to start the program as a public variable. After I did that and modified the code, the values were written properly to the array.
If anyone has other solutions, though, I would like to know. I'm not that experienced with VBA, so I want to hear other solutions.

How to fill-up cells within a Excel worksheet from a VBA function?

I simply want to fill-up cells in my spreadsheet from a VBA function. By example, I would like to type =FillHere() in a cell, and in result I will have a few cells filled-up with some data.
I tried with such a function:
Function FillHere()
Dim rngCaller As Range
Set rngCaller = Application.Caller
rngCaller.Cells(1, 1) = "HELLO"
rngCaller.Cells(1, 2) = "WORLD"
End Function
It breaks as soon as I try to modify the range. Then I tried this (even it's not really the behavior I'm looking for):
Function FillHere()
Dim rngCaller As Range
Cells(1, 1) = "HELLO"
Cells(1, 2) = "WORLD"
End Function
This is not working neither. But it works if I start this function from VBA using F5! It seems it's not possible to modify anything on the spreadsheet while calling a function... some libraries do that though...
I also tried (in fact it was my first idea) to return a array from the function. The problem is that I only get the first element in the array (there is a trick that implies to select a whole area with the formula at the top left corner + F2 + CTRL-SHIFT-ENTER, but that means the user needs to know by advance the size of the array).
I'm really stuck with this problem. I'm not the final end-user so I need something very easy to use, with, preferably, no argument at all.
PS: I'm sorry I asked this question already, but I wasn't registered at that time and it seems that I can't participate to the other thread anymore.
You will need to do this in two steps:
Change your module to be something like:
Dim lastCall As Variant
Dim lastOutput() As Variant
Function FillHere()
Dim outputArray() As Variant
ReDim outputArray(1 To 1, 1 To 2)
outputArray(1, 1) = "HELLO"
outputArray(1, 2) = "WORLD"
lastOutput = outputArray
Set lastCall = Application.Caller
FillHere = outputArray(1, 1)
End Function
Public Sub WriteBack()
If IsEmpty(lastCall) Then Exit Sub
If lastCall Is Nothing Then Exit Sub
For i = 1 To UBound(lastOutput, 1)
For j = 1 To UBound(lastOutput, 2)
If (i <> 1 Or j <> 1) Then
lastCall.Cells(i, j).Value = lastOutput(i, j)
End If
Next
Next
Set lastCall = Nothing
End Sub
Then in order to call the Sub go into the ThisWorkbook area in VBA and add something like:
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Call WriteBack
End Sub
What this does is return the value of the topleft cell and then after calculation completes populates the rest. The way I wrote this it assumes only one FillHere function will be called at a time. If you want to have multiple ones which recalculate at the same time then you will need a more complicated set of global variables.
One word of warning is that this will not care what it overwrites when it populates the other cells.
Edit:
If you want to do this on a Application wide basis in an XLA. The code for the ThisWorkbook area should be something like:
Private WithEvents App As Application
Private Sub App_SheetCalculate(ByVal Sh As Object)
Call WriteBack
End Sub
Private Sub Workbook_Open()
Set App = Application
End Sub
This will wire up the Application Level calculation.
What you're trying to do won't work in Excel - this is by design.
You can do this, though:
Function FillHere()
Redim outputArray(1 To 1, 1 To 2)
outputArray(1, 1) = "HELLO"
outputArray(1, 2) = "WORLD"
FillHere = outputArray
End Function
If you then select two adjacent cells in your worksheet, enter =FillHere() and press Control+Shift+Enter (to apply as an array formula) then you should see your desired output.
Fundamentally, a function can only affect the cell it is called from. It sounds like you may need to look at using the Worksheet_Change or Worksheet_SelectionChange events to trigger the modification of cells in the intended range.
You can do this indirectly using a 2-stage process:
Write your UDF so that it stores data in a sufficiently persistent way (for example global arrrays).
then have an Addin that contains application events that fire after each calculation event, looks at any data stored by the UDFs and then rewrites the neccessary cells (with warning messages about overwrite if appropriate) and reset the stored data.
This way the user does not need to have any code in their workbook.
I think (but do not know for sure) that this is the technique used by Bloomberg etc.

Resources