Sub domain validation and creation from backend - ruby-on-rails-3

Hi i have an application in which user registers and provide company name and i will use that name and create a sub domain which will get saved in company table. Now i want to make it unique as i don't want to restrict user to type same company name which is already in the database. I want that if company name is already present for example: name entered is XYZ so sub domain will be xyz.myapp.com, and when any other user type the same name than sub domain should be xyz1.myapp.com and same for other same names in sequence.
Here i have tried some but it fails on second turn:
def get_available_subdomain
generated_subdomain = name.downcase.gsub(/\s+|\&|\#|\#|\(|\)|\/|\.|\/|\?|\!|\"|\$|\%|\'|\*|\+|\,|\:|\;|\<|\>|\[|\]|\^|\`|\{|\}|\||\-|\~/, "") unless name.blank?
companies = Company.where(:subdomain=> generated_subdomain)
if companies.count == 0
new_subdomain = generated_subdomain
else
new_subdomain = generated_subdomain + (companies.count).to_s
end
new_subdomain
end
How to make a unique method to get the expected results. Any help will be appreciated.

If you are using mysql as a back-end then you can try this for searching companies with generated sud-domain like 'xyz', 'xyz1', 'xyz2' etc.
def get_available_subdomain
generated_subdomain = name.downcase.gsub(/\s+|\&|\#|\#|\(|\)|\/|\.|\/|\?|\!|\"|\$|\%|\'|\*|\+|\,|\:|\;|\<|\>|\[|\]|\^|\`|\{|\}|\||\-|\~/, "") unless name.blank?
companies = Company.where('subdomain REGEXP ?', "#{generated_subdomain}[/d]*")
if companies.count == 0
new_subdomain = generated_subdomain
else
new_subdomain = generated_subdomain + (companies.count).to_s
end
new_subdomain
end
if database is progress then search the companies like this
companies = Company.where('subdomain ~* ?', "#{generated_subdomain}[/d]*")
if database not a mysql then I will suggested that search the company name instead of sub-domain. because you are creating the subdomain according to the company name.
companies = Company.where(:name => generated_subdomain)

Related

Access 2010 - Dynamically Set Combo Control Source and Row Source

I am limited in that I have to stick with the old (messy) table design. On this one interface I am developing, there is a combo drop down that is a fixed values list. The options are Group1, Group2, and Group 3. Based on that, a dependent combo needs to be set to the correct row source and control source.
The form is locked, and the user can navigate the records (first, last, next previous, and an unbound combo to jump to a record). During those operations, I just want to refresh which list the dependent combo should pull from, and what value it should bind to, to show the correct value.
When the user clicks to make a "new" record, I won't show the dependent drop down (or make it enabled. haven't decided yet) until they choose a value in the first combo, telling us which group it is.
In order to control this, I created a function for use on this form. This is that function:
Private Function MyGroup()
'Mybitval is a table value that is a bit data type. It means group1
If Me.Mybitval = True Then
Me.cboGroupType.Value = "Group1"
ElseIf Me.Mybitval = False And Nz(Me.MyID1, 0) <> 0 Then
Me.cboGroupType.Value = "Group2"
ElseIf Me.Mybitval = False And Nz(Me.MyID1, 0) = 0 Then
Me.cboGroupType.Value = "Group3"
End If
Select Case Me.cboGroupType
Case "Group1"
Me.cboGroupName.RowSource = "SELECT Group1.ID, Group1.G1Name FROM Group1 ORDER BY Group1.G1Name;"
Me.cboGroupName.ControlSource = me.FKID1
Case "Group2"
Me.cboGroupName.RowSource = "SELECT Group2.ID, Group2.G2Name FROM Group2 ORDER BY Group2.G2Name;"
Me.cboGroupName.ControlSource = Me.FKID2
Case "Group3"
Me.cboGroupName.RowSource = "SELECT Group3.G3ID, Group3.G3Name FROM Group3 ORDER BY Group3.G3Name;"
Me.cboGroupName.ControlSource = me.FKID1
End Select
Debug.Print Me.cboGroupName.RowSource
Debug.Print Me.cboGroupName.ControlSource
Debug.Print Me.cboGroupName.Value
End Function
The idea is that I call this function on load, after I go to the first record, and then on any navigation button click or after update of the go to combo.
The problem is, that the cbogroupname never shows the value it has in the control source. When I try to load the form, my debug rowsource shows the correct sql, and the control source shows the value of the correct table field for the current record it's on, but 2 things don't seem to work. If I don't do the 3rd debug on the value, the form loads fine, but the combo doesn't show the right value, even though it's bound to a table column that has a value. If do debug the value, I get a run-time error '2424': "The expression you entered has a field, control, or property name that [my application] can't find."
When I hit debug, it goes to the last line of the function:
Debug.Print Me.cboGroupName.Value
When I go to save a new record, I run through validation, and having this 1 combo, instead of 3 different ones with alternating the visibility, seems way easier. Is there a reason this isn't working the way I am wanting it to?
Thanks!
And just like that, I figured it out. Of course it takes me all day, and posting this question to get it.
Here is the function, with the right syntax:
Private Function MyGroup()
If Me.Mybitval = True Then
Me.cboGroupType.Value = "Group1"
ElseIf Me.Mybitval = False And Nz(Me.MyID1, 0) <> 0 Then
Me.cboGroupType.Value = "Group2"
ElseIf Me.Mybitval = False And Nz(Me.MyID1, 0) = 0 Then
Me.cboGroupType.Value = "Group3"
End If
Select Case Me.cboGroupType
Case "Group1"
Me.cboGroupName.RowSource = "SELECT Group1.ID, Group1.G1Name FROM Group1 ORDER BY Group1.G1Name;"
Me.cboGroupName.ControlSource = "FKID1"
Case "Group2"
Me.cboGroupName.RowSource = "SELECT Group2.ID, Group2.G2Name FROM Group2 ORDER BY Group2.G2Name;"
Me.cboGroupName.ControlSource = "FKID2"
Case "Group3"
Me.cboGroupName.RowSource = "SELECT Group3.G3ID, Group3.G3Name FROM Group3 ORDER BY Group3.G3Name;"
Me.cboGroupName.ControlSource = "FKID1"
End Select
Debug.Print Me.cboGroupName.RowSource
Debug.Print Me.cboGroupName.ControlSource
Debug.Print Me.cboGroupName.Value
End Function
Provided your form is bound to a table, such as mine is, the control source needs to = "fieldname" . It needs to be the field name in double quotes. Not me.fieldname as that ends up making the controlsource the value of that field name. It also can't be [table1]![field1] I tried that and that doesn't work either.
The above works beautifully. Now to test the rest of the form!

Access 2010 set variable to single field of a table

I feel like this should be easy, but I haven't been able to come up with a clear answer for this after searching off and on all day.
I have a users table that has email addresses in it.
I have a combo box that references this table.
All I want to do, is set the email address field of the selected user to a string, so I can then do things with that.
just trying to get a string back from a sql query like :
"SELECT emailAddress FROM tblUsers WHERE id = " & Me.cmbUser.Value & ""
Can someone point me in the right direction here?
You can use Combo Box to return email address after user select user from list.
Assume your user tblUsershas User and Email fields
Set your combo box property:
1.Row Source = SELECT User, Email FROM tblusers
2.Column Count = 2
3.Bound Column = 0 (0 is the first column and 1 is the second column which is email)
4.Column width = x";0" (x is your combox box width)
You can get email address from me.combobox.column(1). Both me.combobox.value and me.comboxbox.column(0) is selected User
If you actually want to execute the query, you have multiple options:
You can use DLookup:
TL;DR:
DLookup("Column", "Table", "Col=Value") will execute SELECT Column FROM Table WHERE Col=Value and return the first row.
For the query from your question, you need to use DLookup like this:
Dim mail As String
mail = Nz(DLookup("emailAddress", "tblUsers", "id = " & Me.cmbUser.Value))
You can load your SQL query with a Recordset.
This makes more sense than DLookup if you need more user data from the table than just the email adress.
Dim mail As String
Dim phone As String
Dim RS As DAO.Recordset
Set RS = CurrentDb.OpenRecordset("SELECT emailAddress, phoneNumber FROM tblUsers WHERE id = " & Me.cmbUser.Value)
If Not RS.EOF Then
mail = Nz(RS("emailAddress"))
phone = Nz(RS("phoneNumber"))
End If
RS.Close
Set RS = Nothing
(in order for this to work, your Access database needs a reference to any version of the Microsoft DAO Object Library...this should already be the case in most newer Access versions)
Note the usage of Nz in both examples - this is necessary if the mail adress can be NULL. Without Nz, the code would crash because of setting a string variable to NULL.

LINQ to Entities .contains is ignoring results with NULL

I am new to the Entity Framework, and am struggling with what I hope is a basic problem. My code is here:
Dim accounts As List(Of STUDENT) =
(From a In SA.STUDENTs
Where (a.MATRIC_NO.Contains(matric) And a.FIRST_NAME.Contains(firstName) And a.MIDDLE_NAMES.Contains(middleName) And a.SURNAME.Contains(lastName) And a.PREFERRED_NAME.Contains(preferredName))
Select a).ToList
The query runs fine, until one of the search fields is NULL in the database. If, for instance, a matric number is entered in the seach interface but middle name is left blank, the query will not return any records if middle name is NULL in the database. If middle name is a blank space in the database then it will return the record.
Can anyone offer any pointers?
Many thanks!
You can add an extra check in your query. For example:
public function filterList(IEnumerable list, string name)
{
var filtered_list = list.Where(x=> x.Name.Contains(name) || string.IsNullorWhitespace(name)).ToList();
return filtered_list;
}
So you can see, if the name variable is empty, all elements will return true, so all elements will be return (no real filter applied).
So basically, you can change all filters from
something.Contains(anotherthing)
to
something.Contains(anotherthing) || string.IsnullOrWhitespace(anotherthing)
(From a In SA.STUDENTs
Where isnull(a.MATRIC_NO.Contains(matric) And a.FIRST_NAME.Contains(firstName) And a.MIDDLE_NAMES.Contains(middleName) And a.SURNAME.Contains(lastName) And a.PREFERRED_NAME.Contains(preferredName))
Select a).ToList
Like this `:select * from tbl where statusid = isnull(#statusid,statusid)
try like this ..
Dim get_rmf_2 = From rmf In t_rmf _
Where Not IsDBNull(rmf!NIVP) AndAlso rmf!NIVP = nivp_rap
this is in VB I think this is works fine
I managed to solve this using a different approach. If there was no value entered for a particular field, leave it out the query. I accomplished this using predicates, as below:
'create the base query
Dim accounts =
(From a In SA.STUDENTs
Select a)
'create predicates for each condition required in the query
If matric <> "" Then
accounts = accounts.Where(Function(m) m.MATRIC_NO.Contains(matric))
End If
If firstName <> "" Then
accounts = accounts.Where(Function(f) f.FIRST_NAME.Contains(firstName))
End If
If middleName <> "" Then
accounts = accounts.Where(Function(mn) mn.MIDDLE_NAMES.Contains(middleName))
End If
If lastName <> "" Then
accounts = accounts.Where(Function(l) l.SURNAME.Contains(lastName))
End If
If preferredName <> "" Then
accounts = accounts.Where(Function(p) p.PREFERRED_NAME.Contains(preferredName))
End If
'execute the query
Dim accountlist = accounts.ToList
'return the results
Return accountlist
If anyone can see anything wrong with this, or any gotchas that I'm unaware of, please let me know! I'm very new to LINQ to Entities, and LINQ in general!

Microsoft Access find field in table with known value

We have a fairly large Oracle database that we are able to connect via Microsoft Access and ODBC with read-only access. We work with a front-end system that does not match the structure behind the scenes and often I need to query the system via Microsoft Access. The problem is that we are not provided any documentation as to structure and the structure needs serious attention. Searching for the field that I need is very time consuming.
With our front end, I'm able to view the values that I want to query, and I know the key fields, but I need to find the field that contains the known value.
If I have a record where I know the value of field "A", and have the value of field "X", is it possible to query field "X"?
Front end shows
Student ID: 12345678
Payments: 23456
Back end
TechID: 12345678
???: 23456
Can I query "???"
You can do this by iterating over the collection of tables, and for each table, the collection of fields.
Open Database
Get all Tables
For Each Table
Get all Fields
For Each Field
If Field type is text ... and
If Field size is not TOO Long ...
Search for string
If found, write to a results bucket
Next
Next
Here is an example of code for cataloging tables (source here)
Public Function GenerateDataDictionary(aDataDictionaryTable As String)
'*** Usage: GenerateDataDictionary("MyDataDictionaryTable")
'*** Extracts the information about the tables for the data dictionary
'*** and inserts it to a table named aDataDictionaryTable
Dim tdf As TableDef, fldCur As Field, colTdf As TableDefs
Dim rstDatadict As Recordset
Dim i As Integer, j As Integer, k As Integer
Set rstDatadict = CurrentDb.OpenRecordset(aDataDictionaryTable)
Set colTdf = CurrentDb.TableDefs
'Go through the database and get a tablename
For Each tdf In CurrentDb.TableDefs
'Do what you want with the table names here.
rstDatadict.AddNew
rstDatadict.Update
rstDatadict.AddNew
rstDatadict![Table] = tdf.NAME
rstDatadict![Field] = "----------------------------"
rstDatadict![Display] = "----------------------------"
rstDatadict![Type] = ""
rstDatadict.Update
rstDatadict.AddNew
rstDatadict![Table] = "Table Description:"
For j = 0 To tdf.Properties.Count - 1
If tdf.Properties(j).NAME = "Description" Then
rstDatadict![Field] = tdf.Properties(j).Value
End If
Next j
rstDatadict.Update
rstDatadict.AddNew
rstDatadict.Update
For i = 0 To tdf.Fields.Count - 1
Set fldCur = tdf.Fields(i)
rstDatadict.AddNew
rstDatadict![Table] = tdf.NAME
rstDatadict![Field] = fldCur.NAME
rstDatadict![Size] = fldCur.Size
Select Case fldCur.Type
Case 1
FieldDataType = "Yes/No"
Case 4
FieldDataType = "Number"
Case 8
FieldDataType = "Date"
Case 10
FieldDataType = "String"
Case 11
FieldDataType = "OLE Object"
Case 12
FieldDataType = "Memo"
Case Else ' Other values.
FieldDataType = fldCur.Type
End Select
rstDatadict![Type] = FieldDataType
For j = 0 To tdf.Fields(i).Properties.Count - 1
If fldCur.Properties(j).NAME = "Description" Then
rstDatadict![DESCRIPTION] = fldCur.Properties(j).Value
End If
If fldCur.Properties(j).NAME = "Caption" Then
rstDatadict![Display] = fldCur.Properties(j).Value
End If
If fldCur.Properties(j).NAME = "Rowsource" Then
rstDatadict![LookupSQL] = fldCur.Properties(j).Value
End If
Next j
rstDatadict.Update
Next i
Debug.Print " " & tdf.NAME
Next tdf
End Function
You can catalog your findings in Access by making a table of field-names which joins to a table of table-names. Then your searches are based on the catalog instead of raw collections.
I reverse-engineered the schema for MAS 90 (with JobOps add-in) this way. There's no map, but I had a read-only ODBC connection which I used in precisely the way you propose. The purchasing accountant would give me a distinctive Product Number and I'd run it through this comprehensive engine. Over time I succeeded in distilling 700 tables comprising 18k fields down to 20 tables and a few hundred fields. That allowed us to export our data.
The answer to your question is simple. No, you cannot do that.
There are two solutions that I can think of. The first is to manually concatenate all the values together and then look for the row that contains the value. This is imperfect, but might work:
select *
from (select t.*, ('|'""col1||'|'||col2+'|' . . .||'|') as allcols
from t
) t
where instr('|23456|', allcols) > 0
This would find any row that has that value in a column. Probably close enough for what you want.
The second is to use UNPIVOT to do essentially the same thing.
I would strongly suggest that you invest a little bit of time to find the mapping between the fields, and then create a view in Oracle that has the field names as seen in the application. It sounds like this would save you a lot of effort in the medium term.

How can i use a record i found in database to perform other operations

I was searching for a record in my database and now that the record is found
I want to know how can I use it
for example
if i want to use the name field of the record to put in text
If rsQ.RecordCount <> 0 Then
' Found it
blnExists = True
NameTxt.Value = Name.Value
Else
MsgBox "not found"
End If
It gives me an Invalid qualifier at this line
NameTxt.Value = Name.Value
You say VBA but not which database you are using. From the above, it looks like you would be better off with DLookUp in Access. Otherwise, you can say:
MyVar = rsQ.Fields("[Name]")
Or
MyVar = rsQ.Fields(3)
BTW Name is a reserved word and should not be used.