Query with a bunch of If - ms-access

I have a query field that gives a result text "Completed" or "Not completed" for a field FuncID. I need to create another expression with IFF(s) that what say that operator level 1-5 they are in based on what FuncID(s) are completed using the below formulas
FuncID 1
FuncID 2
FuncID 3
FuncID 4
FuncID 5
FuncID 6
FuncID 7
FuncID 8
Operator 1 =
FuncID 1 OR FuncID 2 has to be completed
Operator 2 =
FuncID 1 AND FuncID 2 has to be completed
Operator 3 =
Four (4) of the eight (8) FuncID(s) has to be completed
Operator 4 =
Six (6) of the eight (8) FuncID(s) has to be completed
Operator 5 =
Eight (8) of the eight (8) FuncID(s) has to be completed
ElegibleLevel: Iff[FuncID]=1 AND [Expr1] = 1,"OPERATOR 1",0,
Iff[FuncID]=1 OR [FuncID]=2 AND [Expr1] = 1,"OPERATOR 1",0,
Iff[FuncID]=1 AND [FuncID]=2 AND [Expr1] = 1,"OPERATOR 2",0,
Iff(Count[FuncID]=3) = 4 AND [Expr1] = 1,"OPERATOR 3",0,
Iff(Count[FuncID]=4) = 6 AND [Expr1] = 1,"OPERATOR 4",0,
Iff(Count[FuncID]=5) = 8 AND [Expr1] = 1,"OPERATOR 5",0,

I suggest to use a database schema similar to this one (simplified):
To determine the operator level of an employee, we need only 2 calculated numbers per employee:
the number of assigned functions out of all 8 possible functions
the number of assigned functions out of only the first 2 functions
The first of these would be your CountOfReqCompleted. Both can be calculated using queries like this:
EmployeesFunctionsAll:
SELECT EmployeesFunctions.EmployeeID,
Count(EmployeesFunctions.FuncID) AS FunctionsCount
FROM EmployeesFunctions
GROUP BY EmployeesFunctions.EmployeeID;
EmployeesFunctionsBasic:
SELECT EmployeesFunctions.EmployeeID,
Count(EmployeesFunctions.FuncID) AS FunctionsCount
FROM EmployeesFunctions
WHERE (((EmployeesFunctions.FuncID) In (1,2)))
GROUP BY EmployeesFunctions.EmployeeID;
Finally, to calculate the operator level for each employee, we can join these 2 queries to the Employees table using a LEFT JOIN such that "all records of 'Employees' are returned". Instead of nesting several Iif functions, we can use the Switch function to accomplish the task:
SELECT Employees.EmployeeID, Employees.EmployeeName,
Switch(
EmployeesFunctionsAll.FunctionsCount >= 8, "Operator 5",
EmployeesFunctionsAll.FunctionsCount >= 6, "Operator 4",
EmployeesFunctionsAll.FunctionsCount >= 4, "Operator 3",
EmployeesFunctionsBasic.FunctionsCount >= 2, "Operator 2",
EmployeesFunctionsBasic.FunctionsCount >= 1, "Operator 1"
) AS OperatorLevel
FROM (Employees LEFT JOIN EmployeesFunctionsAll
ON Employees.EmployeeID = EmployeesFunctionsAll.EmployeeID)
LEFT JOIN EmployeesFunctionsBasic
ON Employees.EmployeeID = EmployeesFunctionsBasic.EmployeeID;

Im now calling a function that will be able to decide based on a count to see if I completed the requirements ect based on my querys outcome instead of all the Iffs.
Function SetLevel(lngFuncID As Long, lngEmpID As Long, varDateCompleted As Variant)
Dim lngPosID As Long
Dim lngEmpPosID As Long
Dim strSQL As String
Dim strCriteria As String
strCriteria = "EmpID = " & lngEmpID
If DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 8 Then
lngPosID = DLookup("PosID", "tblMetalShopLevel", "Position = ""Operator 5""")
ElseIf DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 7 Then
lngPosID = 0
ElseIf DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 6 Then
lngPosID = DLookup("PosID", "tblMetalShopLevel", "Position = ""Operator 4""")
ElseIf DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 5 Then
lngPosID = 0
ElseIf DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 4 Then
lngPosID = DLookup("PosID", "tblMetalShopLevel", "Position = ""Operator 3""")
ElseIf DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 3 Then
lngPosID = 0
ElseIf DCount("*", "qryMetalShopEmployeeFunctions", _
strCriteria & " And (FuncID = 1 Or FuncID = 2)") = 2 Then
lngPosID = DLookup("PosID", "tblMetalShopLevel", "Position = ""Operator 2""")
ElseIf lngFuncID = 1 Or lngFuncID = 2 Then
lngPosID = DLookup("PosID", "tblMetalShopLevel", "Position = ""Operator 1""")
End If
If lngPosID > 0 Then
lngEmpPosID = Nz(DMax("EmpPosID", "tblMetalShopEmployeeLevel"), 0) + 1
strSQL = "INSERT INTO tblMetalShopEmployeeLevel(EmpPosID, EmpID, PosID, DateAchieved) " & _
"VALUES(" & lngEmpPosID & "," & lngEmpID & "," & lngPosID & "," & _
IIf(IsNull(varDateCompleted), "NULL", "#" & Format(varDateCompleted, "yyyy-mm-dd hh:nn:ss") & "#") & ")"
CurrentDb.Execute strSQL, dbFailOnError
End If
End Function

Related

Null textboxs on subform stopping VB until parent choice is made

I am getting a runtime error
Run-Time error 2427 You entered an expression that has no value.
I know why I am getting it I
just dont know how to fix it. ctrl1 and ctrl2 are on a subform and are two text boxes that do some If statements
on the form after this is ran so the text boxes are null or dont even have data yet. They are blank on the subform.
Here is my code:
Dim ctrl1 As Control
Dim ctrl2 As Control
Set ctrl1 = Me.Parent.frmRequirementsSubform.Form.txtSumOfCompleted
Set ctrl2 = Me.Parent.frmRequirementsSubform.Form.txtTotalRequirementsNeeded
If ctrl1 = ctrl2 Then
Call SetLevel(cboArea, txtEmpID, txtDateFunctionCompleted)
End If
Here is the Function it is calling...
Function SetLevel(lngFuncID As Long, lngEmpID As Long, varDateCompleted As Variant)
Dim lngPosID As Long
Dim lngEmpPosID As Long
Dim strSQL As String
Dim strCriteria As String
strCriteria = "EmpID = " & lngEmpID
If DCount("*", "tblEmployeeFunctions", strCriteria) = 8 Then
lngPosID = DLookup("PosID", "tblLevel", "Position = ""Operator 5""")
ElseIf DCount("*", "tblEmployeeFunctions", strCriteria) = 7 Then
lngPosID = 0
Exit Function
ElseIf DCount("*", "tblEmployeeFunctions", strCriteria) = 6 Then
lngPosID = DLookup("PosID", "tblLevel", "Position = ""Operator 4""")
ElseIf DCount("*", "tblEmployeeFunctions", strCriteria) = 5 Then
lngPosID = 0
Exit Function
ElseIf DCount("*", "tblEmployeeFunctions", strCriteria) = 4 Then
lngPosID = DLookup("PosID", "tblLevel", "Position = ""Operator 3""")
ElseIf DCount("*", "tblEmployeeFunctions", strCriteria) = 3 Then
lngPosID = 0
Exit Function
ElseIf DCount("*", "tblEmployeeFunctions", _
strCriteria & " And (FuncID = 1 Or FuncID = 2)") = 2 Then
lngPosID = DLookup("PosID", "tblLevel", "Position = ""Operator 2""")
ElseIf lngFuncID = 1 Or lngFuncID = 2 Then
lngPosID = DLookup("PosID", "tblLevel", "Position = ""Operator 1""")
End If
'Debug.Print "lngPosID: " & lngPosID
If lngPosID > 0 Then
lngEmpPosID = Nz(DMax("EmpPosID", "tblEmployeeLevel"), 0) + 1
strSQL = "INSERT INTO tblEmployeeLevel(EmpPosID, EmpID, PosID, DateAchieved) " & _
"VALUES(" & lngEmpPosID & "," & lngEmpID & "," & lngPosID & "," & _
IIf(IsNull(varDateCompleted), "NULL", "#" & Format(varDateCompleted, "yyyy-mm-dd") & "#") & ")"
CurrentDb.Execute strSQL, dbFailOnError
End If
End Function
If the controls are on the subform, just reference them straight out, Me.TextBox1 no need to crawl up to Me.Parent.SubForm.Form.TextBox1
If I have to guess you are using somewhere a function which doesnt allow Null values. #C Perkins is right we need more details about the error.
But you can make a check in your code if the textboxes are Null, and if not execute the code.
If IsNull(ctrl1) or IsNull(ctrl2) Then
ExitSub
Else
'Do Stuff
End If
First, frmRequirementsSubform must be the name of the subform control (could be different from the name of the subform).
Then, insert some lines to show the values, and tell us what you see:
Debug.Print "ctrl1: >" & ctrl1.Value & <", ctrl2: >" & ctrl2.Value & "<"
If ctrl1.Value = ctrl2.Value Then
Debug.Print "cboArea: >" & cboArea & "<, txtEmpID: >" & txtEmpID & "<, txtDateFunctionCompleted: >" & txtDateFunctionCompleted & "<"
Call SetLevel(cboArea, txtEmpID, txtDateFunctionCompleted)
End If
To compare also Null values, use for text:
If Nz(ctrl1.Value, "") = Nz(ctrl2.Value, "") Then
or, for numeric:
If Nz(ctrl1.Value, 0) = Nz(ctrl2.Value, 0) Then
Edit:
To check also for empty objects:
If Not (ctrl1 Is Nothing Or ctrl2 Is Nothing) Then
If Nz(ctrl1.Value, 0) = Nz(ctrl2.Value, 0) Then
Function SetLevel(lngFuncID As Long, lngEmpID As Long, varDateCompleted As Variant)
Dim lngPosID As Long
Dim lngEmpPosID As Long
Dim strSQL As String
Dim strCriteria As String
strCriteria = "EmpID = " & lngEmpID
If DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 8 Then
lngPosID = DLookup("PosID", "tblMetalShopLevel", "Position = ""Operator 5""")
ElseIf DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 7 Then
lngPosID = 0
ElseIf DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 6 Then
lngPosID = DLookup("PosID", "tblMetalShopLevel", "Position = ""Operator 4""")
ElseIf DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 5 Then
lngPosID = 0
ElseIf DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 4 Then
lngPosID = DLookup("PosID", "tblMetalShopLevel", "Position = ""Operator 3""")
ElseIf DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 3 Then
lngPosID = 0
ElseIf DCount("*", "qryMetalShopEmployeeFunctions", _
strCriteria & " And (FuncID = 1 Or FuncID = 2)") = 2 Then
lngPosID = DLookup("PosID", "tblMetalShopLevel", "Position = ""Operator 2""")
ElseIf lngFuncID = 1 Or lngFuncID = 2 Then
lngPosID = DLookup("PosID", "tblMetalShopLevel", "Position = ""Operator 1""")
End If
If lngPosID > 0 Then
lngEmpPosID = Nz(DMax("EmpPosID", "tblMetalShopEmployeeLevel"), 0) + 1
strSQL = "INSERT INTO tblMetalShopEmployeeLevel(EmpPosID, EmpID, PosID, DateAchieved) " & _
"VALUES(" & lngEmpPosID & "," & lngEmpID & "," & lngPosID & "," & _
IIf(IsNull(varDateCompleted), "NULL", "#" & Format(varDateCompleted, "yyyy-mm-dd hh:nn:ss") & "#") & ")"
CurrentDb.Execute strSQL, dbFailOnError
End If
End Function

how to display long text ( datatype text up to 20 000 characters) in div

I'm currently enhancing a system using vb.net. My issue is, i need to display a column name 'WONOTE' (datatype TEXT) from SQL Server into div in html front screen. The maximum length of characters for this column is up to 22 000 characters. I retrieved the data from SQL server into div by using sql command in code behind. I manage to display the data but only up to 110 characters by using this statement 1:
REPLACE(REPLACE(cast(WONOTE as varchar(110)), CHAR(13), ''), CHAR(10), '')
and up to 10 characters using this statement 2:
CONVERT(VARCHAR(10), b.WONOTE) as WONOTE
but I need it to display full text. If i change into varchar(max) or anything greater than 110 for statement 1 and 10 for statement 2 it display nothing.
I wish someone can help me with it.
Thank you in advance.
How i retrieved data from SQL server:
Public Sub GETWHATSRUNNING()
Dim paraWC As SqlParameter
Dim SQL As String
Dim myArray, myArray1, myArray2, myArray3,
myArray4, myArray5, myArray6, myArray7,
myArray8, myArray9, myArray10 As String
TempDT.Columns.Add("WO", GetType(String))
TempDT.Columns.Add("WOQTY", GetType(String))
TempDT.Columns.Add("PartNum", GetType(String))
TempDT.Columns.Add("Desc", GetType(String))
TempDT.Columns.Add("WIPQTY", GetType(String))
TempDT.Columns.Add("WIPDAYS", GetType(String))
TempDT.Columns.Add("WOAGING", GetType(String))
TempDT.Columns.Add("AGINGATWC", GetType(Double))
TempDT.Columns.Add("COLOR", GetType(String))
'TempDT.Columns.Add("WO_NOTE", GetType(String))
WCLimit = 5
SQL = "select distinct A.WONO, B.BLDQTY , C.PART_NO , C.DESCRIPT, B.Start_Date, REPLACE(REPLACE(cast(WONOTE as varchar(110)), CHAR(13), ''), CHAR(10), '') " & _
"from Transfer A " & _
"left join WOENTRY B on A.wono = B.wono " & _
"left join INVENTOR C on B.UNIQ_KEY = C.UNIQ_KEY " & _
"where FR_DEPT_ID = #WC and start_date is not null " & _
"and B.BLDQTY <> B.COMPLETE "
GetConnection()
oConnSql = New SqlConnection(connString.ToString)
oCmdSql = New SqlCommand(SQL, oConnSql)
paraWC = New SqlParameter("#WC", SqlDbType.VarChar, 5)
paraWC.Value = lblWC.Text
oCmdSql.Parameters.Add(paraWC)
oCmdSql.CommandTimeout = 7200
Try
If oConnSql.State = ConnectionState.Closed Then
oConnSql.Open()
End If
' Adapter and Dataset
oAdptSql.SelectCommand = oCmdSql
oAdptSql.Fill(oDS, "dtList")
oAdptSql.Fill(dt)
If dt.Rows.Count > 0 Then
Dim ProgessQty, WIPQty, WOQuantity As String
Dim AgingWC, WOAging As Double
'Dim WCAge, WoAge As TimeSpan
Dim LeadTime As Double
Dim Holiday As Integer
Dim counter As Integer = 1
Dim count As Integer = dt.Rows.Count - 1
For i = 0 To count - 1
ProgessQty = GETProgressWOQuantity(Trim(dt.Rows(i)(0).ToString))
WOQuantity = Trim(dt.Rows(i)(1).ToString)
WIPQty = CInt(ProgessQty)
LeadTime = GetLeadTime(Trim(dt.Rows(i)(2).ToString), lblWC.Text)
Holiday = CheckForHolidays(CDate(dt.Rows(i)(4).ToString), Now())
WOAging = Format((DateDiff(DateInterval.Minute, CDate(dt.Rows(i)(4).ToString), Now())) / 60 / 24, "0.0") - Holiday
AgingWC = WOAging - LeadTime
If AgingWC >= 5 And WIPQty > 0 Then
TempDT.Rows.Add(Trim(dt.Rows(i)(0).ToString), WOQuantity, Trim(dt.Rows(i)(2).ToString), Trim(dt.Rows(i)(3).ToString), WIPQty, Trim(dt.Rows(i)(5).ToString), Math.Round(CDbl(WOAging), 2), Math.Round(CDbl(AgingWC), 2), IIf(Math.Round(CDbl(AgingWC), 2) >= WCLimit, "Red", "Black"))
'
counter += 1
Else
End If
Next
Dim dataView As New DataView(TempDT)
dataView.Sort = " AGINGATWC DESC"
SortDT = dataView.ToTable()
For j = 0 To SortDT.Rows.Count - 1
myArray = myArray & "|" & j + 1
myArray1 = myArray1 & "|" & Trim(SortDT.Rows(j)(0).ToString) 'WO
myArray2 = myArray2 & "|" & Trim(SortDT.Rows(j)(1).ToString) 'WO QTY
myArray3 = myArray3 & "|" & Trim(SortDT.Rows(j)(2).ToString) 'Part Number
myArray4 = myArray4 & "|" & Trim(SortDT.Rows(j)(3).ToString) 'Description
myArray5 = myArray5 & "|" & Trim(SortDT.Rows(j)(4).ToString) 'WIP QTY
myArray6 = myArray6 & "|" & Trim(SortDT.Rows(j)(5).ToString) 'WIP DAYS
myArray7 = myArray7 & "|" & Trim(SortDT.Rows(j)(6).ToString) 'WO Aging
myArray8 = myArray8 & "|" & Trim(SortDT.Rows(j)(7).ToString) 'Aging at WC
myArray9 = myArray9 & "|" & Trim(SortDT.Rows(j)(8).ToString) 'Color
myArray10 = myArray10 & "|" & Trim(SortDT.Rows(j)(5).ToString) 'WONOTE
Next
dt.Clear()
dt.Dispose()
oCmdSql.Dispose()
oConnSql.Close()
ViewState.Clear()
ViewState("JArray") = myArray
ViewState("JArray1") = myArray1
ViewState("JArray2") = myArray2
ViewState("JArray3") = myArray3
ViewState("JArray4") = myArray4
ViewState("JArray5") = myArray5
'ViewState("JArray6") = myArray6
ViewState("JArray7") = myArray7
ViewState("JArray8") = myArray8
ViewState("JArray9") = myArray9
ViewState("JArray10") = myArray10
End If
Catch ex As Exception
lblResult.Text = "Exception Message: " + ex.Message
Finally
End Try
End Sub
Now I realised if I run in Internet Explorer with varchar(max) it says

Access Update Query using VBA

I have a Table like this
SNo Block SAP SAG BAP BAG DEP DEG
1 600403 1 3 5 4
2 600405 1 3 1 3 1 1
3 600407 3 1 2 4
4 600409 3 1
5 600410 1 3 2 5 1 3
6 600413 1 4 1 3
I want to NULL the Cells of SAP and SAG where SAP = 1 and SAG = 3, and null the cells of BAP and BAG where BAP = 1 and BAG = 3 and like wise for DEP and DEG, i am expecting result like this below
SNo Block SAP SAG BAP BAG DEP DEG
1 600403 5 4
2 600405 1 1
3 600407 3 1 2 4
4 600409 3 1
5 600410 2 5
6 600413 1 4
Some how after googling, I wrote a code for this, and the code runs successfully without any error but only the SAP Column gets NULLed and the SAG column was not NULLed (the second Query for SAG docmd doesn't work) !
Below is my VBA, Sorry I am new to Access VBA !
Private Sub VbaModule()
Dim db As DAO.Database
Dim rs As Recordset
Dim sSQL As String
Dim sSQL1 As String
Set db = CurrentDb()
Set rs = db.OpenRecordset("T05_Pr2_Null_Not_In_Rem")
sSQL = "UPDATE T05_Pr2_Null_Not_In_Rem SET SAP = NULL " & _
" WHERE (SAP = 1 AND SAG = 3)"
DoCmd.RunSQL sSQL
sSQL = "UPDATE T05_Pr2_Null_Not_In_Rem SET SAG = NULL " & _
" WHERE (SAP = 1 AND SAG = 3)"
DoCmd.RunSQL sSQL
rs.Close
Set rs = Nothing
db.Close
End Sub
Any Suggestion ?
Dim strSQL As String
strSQL = "UPDATE T05_Pr2_Null_Not_In_Rem " & _
"SET " & _
" SAP = NULL, " &
" SAG = NULL " & _
"WHERE SAP = 1 AND SAG = 3;"
CurrentDb.Execute strSQL, dbFailOnError
strSQL = "UPDATE T05_Pr2_Null_Not_In_Rem " & _
"SET " & _
" BAP = NULL, " &
" BAG = NULL " & _
"WHERE BAP = 1 AND BAG = 3;"
CurrentDb.Execute strSQL, dbFailOnError
strSQL = "UPDATE T05_Pr2_Null_Not_In_Rem " & _
"SET " & _
" DEP = NULL, " &
" DEG = NULL " & _
"WHERE DEP = 1 AND DEG = 3;"
CurrentDb.Execute strSQL, dbFailOnError

Guidance, VBA - SELECT CASE clean up

This SELECT CASE scenario is working for me but I think the code can be more friendly ... any advice would be very helpful.
Select Case True 'select case where worker name and action is true then in each case RSworkhours.addnew
Case Me.Worker1.Value <> "" And Me.fw1a1 = 1
With RsWorkHours
.AddNew
!WorkerID = Me.Worker1
!Date = Me.TxtDate
!StandardTime = Me.w1a1s
!Overtime = Me.w1a1o
!Doubletime = Me.w1a1d
!ScaffoldID = Me.cboScaffnum
.Update
End With
Me.fw1a1 = 0
GoTo WorkerHours
Case Me.Worker1.Value <> "" And Me.fw1a2 = 1
With RsWorkHours
.AddNew
!WorkerID = Me.Worker1
!Date = Me.TxtDate
!StandardTime = Me.w1a2s
!Overtime = Me.w1a2o
!Doubletime = Me.w1a2d
!ScaffoldID = Me.cboScaffnum
.Update
End With
Me.fw1a2 = 0
GoTo WorkerHours
The Code iterates through this Select Case 80 times, if there are 16 workers and each have 5 actions.
I was thinking maybe having a loop that modifies the number within the arguments like:
for each x to 16
for each y to 5
If Me.worker & x & .Value <> "" And Me.fw & x & a & y Then
With Recordset
.AddNew
'insert stuff
.Update
End With
End If
Next y
Next x
Does anyone have any insight?
Thank you in advance.
-Matt
You can access all controls by their name from the Controls collection.
Just pass the name of a control and you will get to that control - the name is a string and can of course be dynamic.
Dim x As Long, y As Long
Dim WorkerX As Control, wXaYs As Control, wXaYo As Control, wXaYd As Control
For x = 1 To 16
For y = 1 To 5
Set WorkerX = Me.Controls("Worker" & x)
Set wXaYs = Me.Controls("w" & x & "a" & y & "s")
Set wXaYo = Me.Controls("w" & x & "a" & y & "o")
Set wXaYd = Me.Controls("w" & x & "a" & y & "d")
If WorkerX.Value > "" And wXaYs.Value > "" Then
With Recordset
.AddNew
!WorkerID = WorkerX.Value
!Date = Me.TxtDate
!StandardTime = wXaYs.Value
!Overtime = wXaYo.Value
!Doubletime = wXaYd.Value
!ScaffoldID = Me.cboScaffnum
.Update
End With
End If
Next y
Next x

all possible combinations

i need to get a list of all possible combinations, not permutations.
to make sure i have the right name, 123 and 321 to me are the same thing and should only be listed once.
the code below does what i need but i can't convert it into MS Access vba.
i'm sorry, i know this is basic and it has been asked a million times but i can't find anything for MS Access that works for me.
Sub test_print_nCr()
print_nCr 7, 3, Range("A1")
End Sub
2.
Public Function print_nCr(n As Integer, r As Integer, p As Range)
c = 1
internal_print_nCr n, r, p, 1, 1
End Function
3.
Public Function internal_print_nCr(n As Integer, r As Integer, ByVal p As Range, Optional i As Integer, Optional l As Integer) As Integer
' n is the number of items we are choosing from
' r is the number of items to choose
' p is the upper corner of the output range
' i is the minimum item we are allowed to pick
' l is how many levels we are in to the choosing
' c is the complete set we are working on
If n < 1 Or r > n Or r < 0 Then Err.Raise 1
If i < 1 Then i = 1
If l < 1 Then l = 1
If c < 1 Then c = 1
If r = 0 Then
p = 1
Exit Function
End If
Dim x As Integer
Dim y As Integer
For x = i To n - r + 1
If r = 1 Then
If c > 1 Then
For y = 0 To l - 2
If p.Offset(c - 1, y) = "" Then p.Offset(c - 1, y) = p.Offset(c - 2, y)
Next
End If
p.Offset(c - 1, l - 1) = x
c = c + 1
Else
p.Offset(c - 1, l - 1) = x
internal_print_nCr n, r - 1, p, x + 1, l + 1
End If
Next
End Function
thank you again
I am not sure if this is the best method to do this, but I would use a kind of binary representation. For instance, consider the word "boy" with the number of letters n=3. This word has three letters, so you can use something like this:
001 = y,
010 = o,
011 = oy,
100 = b,
101 = by,
110 = bo,
111 = boy.
The left side can be done with a loop from i=1 to power(2,n)-1 and transforming i to a number in the binary basis. So, the only thing you have to do is to use the non null positions to build your combinations.
Probably there is something more interesting than this in Knuth.
i found this code here, and it gives me exactly what i need. you just have to create a table with numbers from 1-100. instructions at the link below
enter link description here
thank you everyone
Public Sub buildquery(strN As String, K As Integer)
Dim qd As DAO.QueryDef
Dim intI As Integer
Dim strsql As String
Dim strSelect As String
Dim strFrom As String
Dim strWhere As String
Set qd = CurrentDb.QueryDefs("QN")
qd.sql = "SELECT N FROM tblN WHERE N IN (" & strN & ")"
Set qd = Nothing
strSelect = "SELECT QN.N "
strFrom = "FROM QN "
strWhere = "WHERE QN_1.N > QN.N "
For intI = 1 To K - 1
strSelect = strSelect & ", QN_" & intI & ".N AS N" & intI & " "
strFrom = strFrom & ", QN AS QN_" & intI & " "
If intI < K - 1 Then
strWhere = strWhere & " AND QN_" & intI + 1 & ".N > QN_" & intI & ".N "
End If
Next
strsql = strSelect & " INTO tblCombinations " & strFrom & strWhere
DoCmd.SetWarnings False
DoCmd.RunSQL strsql
DoCmd.SetWarnings True
End Sub
then test
Public Sub testbuildquery()
buildquery "1,2,3,4,5,6,7", 3
End Sub