In the recent versions Excel VBA, the VB engine converts an Integer data type to Long while maintaining the Integer overflow limits. So as a rule of thumb, I always use Long values in Excel VBA.
Does anyone know if the same is true in Access? I realize it's the same engine with different references, but as I move more into Access VBA I never want to assume.
Be careful with your assumptions.
See the following:
Sub checkForVariableMemorySizes()
Dim arrInt() As Integer
ReDim arrInt(1 To 2)
arrInt(1) = 12
arrInt(2) = 456
Dim arrLong() As Long
ReDim arrLong(1 To 2)
arrLong(1) = 12
arrLong(2) = 456
Debug.Print "Integer size:" & vbTab & VarPtr(arrInt(2)) - VarPtr(arrInt(1)) & vbTab & vbTab & "Typename: " & TypeName(arrInt(2))
Debug.Print "Long size:" & vbTab & vbTab & VarPtr(arrLong(2)) - VarPtr(arrLong(1)) & vbTab & vbTab & "Typename: " & TypeName(arrLong(2))
End Sub
this outputs:
Integer size: 2 Typename: Integer
Long size: 4 Typename: Long
You will notice that the sizes are actually different (measured by number of bytes each is incremented, 2 and 4 respectively).
Identical behavior exists for this automatic conversion as the same output from both Excel and Access (I am using Access/Excel 2010 on 32-bit Windows 7). I would be very interested to see what this outputs in older versions.
No, it is not the same in Access.
If you are dealing with numbers larger than the limits of int, you need to use long.
As just a side note, although excel VBA may do the conversion, it is not a good practice.
MS Access Q&A - Overflow Error
Related
I am trying to build this query through VBA instead of building it in Access and running a docmd.openquery. That seemed to me like the easier route, but I wanted to work on my SQL. Obviously that didn't work as intended if I am here lol.
So, I am trying to take the Date values of 14 text boxes on our JobTicket form and insert them into another table, Tbl_Schedule. This table is not a part of the Query that is the record source for the JobTicket form. I am worried that attempting to add this table in will overload the Query, as it is already very full. When I try to quickly navigate to the last field in that Query the text writes on top of itself, and then Access goes not responding while it clears up the text and loads the last couple fields. Adding another 56 fields to that seems like a recipe for disaster. I will post the SQL I have written below.
DoCmd.RunSQL "INSERT INTO Tbl_Schedule (Date_Scheduled1, Date_Scheduled2, Date_Scheduled3, Date_Scheduled4, Date_Scheduled5, Date_Scheduled6, Date_Scheduled7, " & _
"(Date_Scheduled8, Date_Scheduled9, Date_Scheduled10, Date_Scheduled11, Date_Scheduled12, Date_Scheduled13, Date_Scheduled14)" & _
"VALUES (#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled1_JobTicket] & "#,#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled2_JobTicket] & "#, " & _
"(#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled3_JobTicket] & "#,#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled4_JobTicket] & "#, " & _
"(#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled5_JobTicket] & "#,#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled6_JobTicket] & "#, " & _
"(#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled7_JobTicket] & "#,#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled8_JobTicket] & "#, " & _
"(#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled9_JobTicket] & "#,#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled10_JobTicket] & "#, " & _
"(#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled11_JobTicket] & "#,#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled12_JobTicket] & "#, " & _
"(#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled13_JobTicket] & "#,#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled14_JobTicket] & "#)"
Table being inserted into: Tbl_Schedule
Fields being inserted into: Date_Scheduled1 -to- Date_Scheduled14
Getting data from text boxes: Txt_DateScheduled1_JobTicket -to- Txt_DateScheduled14_JobTicket on Frm_JobTicket
Any other questions that would assist you in assisting me please feel free to ask! Thanks in advance!
Dynamic SQL has its uses, but this is not one of them.
Using DAO methods makes your code so much simpler and easier to read and debug.
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim frm As Access.Form
' for readability
Set frm = Forms!Frm_JobTicket
' open table for adding record(s)
Set db = CurrentDb
Set rs = db.OpenRecordset("Tbl_Schedule", dbOpenDynaset, dbAppendOnly)
rs.AddNew
rs!Date_Scheduled1 = frm!Txt_DateScheduled1_JobTicket
rs!Date_Scheduled2 = frm!Txt_DateScheduled2_JobTicket
' etc.
rs.Update
rs.Close
With enumerated field names like these, you can also use a loop:
Dim i As Long
rs.AddNew
For i = 1 To 14
rs("Date_Scheduled" & i) = frm("Txt_DateScheduled" & i & "_JobTicket")
Next i
rs.Update
This is a good opportunity to consider normalizing your data so that part of your problem is removed entirely. Instead of having DateScheduled1_JobTicket, DateScheduled2_JobTicket etc., it might be better to have another table which fills vertically instead of horizontally, perhaps with fields like ID, Item, JobTicketNumber, ScheduledDate.
Then, fill this table with a row for each item/sku/product, and date. You'll have 14 rows for scheduled tickets for each item/sku/product instead of 14 columns, and this will also solve your future problem of adding 56 fields. The benefit is that you can present the job ticket schedule rows by using continuous forms (in a list). Even better, you can put this continuous form with dates as a subform on your item/sku/product main form, which will then show as a neat list of scheduled tickets that will automatically change as you scroll through item/sku/products.
If you don't use continuous forms, you can still use an unbound approach as you're using now. One benefit is that it will be much easier when you need to add future JobTicket numbers, since you can just add more rows instead of adding fields and having to do additional design work.
If you want to view data in the flattened way that you built your table, you can use a Crosstab query to present it as you have in your table, but the underlying data will be much better stored in a normalized format.
Note that you don't need to concatenate a string as you did above; just leave the Forms!Form!Control reference expression directly in the query and you have a nice parameterized query that will execute just fine, so long as there are dates in those controls (text box, drop down etc).
ex.
Insert Into (MyDateField) Values (Forms!MyForm!MyDateControl);
No dynamic SQL needed.
I am currently trying to make a simple VBA program that would multiply two numbers together and then prompt a message box that says "The value of (variable1) and (variable 2) is (The answer).
Here is my current attempt that isn't perfect:
MsgBox prompt:=intmlt, Title:="The Value of " & intFirstNum & intSecondNum
The two big issues I have is how to you put a space inbetween the intFirstNum and intSecondNum? and How do you add an is to the end of that prompt?
It should be
"The Value of " & intFirstNum & " and " & intSecondNum & " is "
Per #ScottHoltzman
I'm trying to read the list of available items in an Enterprise Custom Field programatically, using MS Project 2010 in an EPM landscape.
I'm customizing the Enterprise Global, thus I'm sticking to VBA.
Some fields are text-fields, whereas some are bound to a Lookup Table (in PWA's Server Settings).
I can read each field's current value using:
?ActiveProject.ProjectSummaryTask.GetField(FieldNameToFieldConstant("_MY_FIELD_NAME_"))
but I cannot get a list of available values that _MY_FIELD_NAME_ may have, case it is bound to a Lookup Table.
I have found several references to:
Application.CustomFieldValueListGetItem(FieldID,Item,Index)
But none of them work. This is a rather common unanswered question being asked around.
I could not find a solution for this yet; has anyone (please) ?
Thank you for your time, any help is most welcome.
I'm excited to share that I've found what I was looking for!
I'm posting it as it may help somebody else.
So I took a few steps back and read in detail all the labels in the "Custom Fields" dialog in MS Proj. I've digged MSDN about the terms that made sense to me, such as LookupTable and OutlineCodes. I was pushing the wrong button! The code below illustrates this.
Sub TestGOC()
'pfonseca Sep 2013
'This Sub exhausts my Immediate Window. Run with caution (or dont run at all)
Exit Sub ' safety plug
For i = 1 To Application.GlobalOutlineCodes.Count
Debug.Print "GOC" & "(" & i & ")" & " " & Application.GlobalOutlineCodes(i).Name
For j = 1 To Application.GlobalOutlineCodes(i).LookupTable.Count
Debug.Print "..." & "(" & j & ")" & " " & Application.GlobalOutlineCodes(i).LookupTable(j).Name
Next j
Next i
End Sub
The function below dumps the allowed values per CEF received. It's a starting point for more elaborated use-cases:
Sub DumpListValues(pCEFName As String)
'pfonseca Sep 2013
For i = 1 To Application.GlobalOutlineCodes.Count
If Application.GlobalOutlineCodes(i).Name = Trim(pCEFName) Then
For j = 1 To Application.GlobalOutlineCodes(i).LookupTable.Count
Debug.Print "(" & j & ")" & " " & Application.GlobalOutlineCodes(i).LookupTable(j).Name
Next j
End If
Next i
End Sub
The same issue as posted in MSDN.
Pedro
Using Attachmate, I am trying to write a VBA script that reacts when a specific phrase occurs and automatically executes commands via inline commands. Essentially, when a phrase appears, an inputbox appears asking the user for a specific quantity and the VBA code takes that quantity, inserts it into the terminal and then jumps around different menus to create an internal label. However, my problem is that I don't know how to have the VBA code react to the different strings that may be returned by the host. Sometimes it says "enter to continue" and sometimes it says "select user". So what I want it to do is based on the statement it receives to do a certain action, but I don't know what the command is for capturing what the terminal is receiving from the host. I've tried "waitforstring" and "readline" but it is obvious I am not using them correctly. Below is the code I have built thus far, please be gentle as it is still very unfinished. I have commented out several parts of it in attempts to troubleshoot my problems:
'variable declarations
Dim count As Long 'var used to indicate how many times code should loop (how many labels should be print)
Dim drugname As String
Dim qtyinput As Long
Dim CR As String ' Chr(rcCR) = Chr(13) = Control-M
Dim LF As String ' Chr(rcLF) = Chr(10) = Control-J
Dim strcheck As String
'assign values to variables
count = 0
CR = Chr(Reflection2.ControlCodes.rcCR)
LF = Chr(Reflection2.ControlCodes.rcLF)
qtyinput = InputBox("Number of items being sent", Quantity)
drugname = .GetText(22, 15, 22, 46) ' StartRow:=22, StartColumn:=15,EndRow:=22, EndColumn:=46 'copies text from screen
' Press EditCopy (Copy the selection and put it on the Clipboard).
'.Copy rcSelection, rcAsPlainText -- not needed
.Transmit qtyinput & CR
.Transmit CR
'strcheck = .readline("00:00:01")
'MsgBox strcheck
'If .WaitForString("Press " & Chr(34) & "RETURN" & Chr(34) & " to continue, " & Chr(34) & "^" & Chr(34) & " to stop: ") Then .Transmit CR
'Select Case strcheck
' Case strcheck Like "to continue"
' .Transmit CR
'Case strcheck Like "*Select CLIENT*"
' .Transmit CR
'End Select
.Transmit "^MED" & CR
.Transmit "3" & CR
.Transmit "10" & CR
First of all, Attachmate is the company, and they have a few products for accessing Mainframe sessions from Windows including EXTRA! and Reflections, both of which share a common scripting language, which is nice and easy to use from within VBA.
However, EXTRA! tends to have fewer commands available to use than Reflections, which is the more expensive product, so you have to get a little creative with your VBA.
I think you are using EXTRA!, so the command you are looking for is "GetString"
I use VBA to interact with a mainframe session in EXTRA!, and I know that my mainframe command is successful when three stars appear on the screen in a certain position.
The mainframe command can take anywhere between 1 second and 5 minutes to complete, so I use "GetString" to poll the mainframe session every second, waiting for the three stars before I continue:
Do Until Sess0.Screen.GetString(14, 2, 3) = "***"
Application.Wait (Now + TimeValue("0:00:01"))
Loop
The syntax for "GetString" is: GetString(Row, Column, Length)
In my case the stars appear at row 14, column 2, and I know there will always be 3 of them, so I set my string length to 3.
I am currently writing some software in visual studio to analyse large amounts of data from an Access database using SQL. I have code to make a new calculated variable but am struggling with the amount of time it takes to write the data back into Access.
I am currently using some vb com code to communicate with my Access Database which is running in 2002/3 comparability mode. The following is my current code which runs a function in a loop to write to the database.
cnnOLEDB = New OleDbConnection
cnnOLEDB.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataDirectoryName & DatabaseFileName
cnnOLEDB.Open()
'cmdOLEDB = New OleDbCommand
cmdOLEDB.Connection = cnnOLEDB
ColumnString = "ID_VAR, ID_PAR, TimeValue, strValue, ID_UPL"
For RecordCounter = 0 To CalcData.GetLength(1) - 1
Var_ID = Var_ID + 1
ValueString = Format(Var_ID, "0") & ", " & Format(Parameter, "0") & ", #" & Date2String(CDate(CalcData(0, RecordCounter))) & "#, " & CalcData(CalcData.GetLength(0) - 1, RecordCounter) & ", " & Format(AsUpload, "0")
If DatabaseConnectionInsert("INSERT INTO " & TableName & " (" & ColumnString & ") VALUES (" & ValueString & ")", "Non-Query") = "Error" Then GoTo Close
Next
cnnOLEDB.Close()
Here is the Function:
Public Function DatabaseConnectioninsert(ByVal Query As String, ByVal Task As String) As String
'On Error GoTo Err
'If cnnOLEDB.State = ConnectionState.Open Then cnnOLEDB.Close()
cmdOLEDB.CommandText = Query
Select Case Task
Case "Read Recordset"
rdrOLEDB = cmdOLEDB.ExecuteReader()
DatabaseConnectioninsert = "Read Recordset"
Case "Read Scalar"
DatabaseConnectioninsert = cmdOLEDB.ExecuteScalar
Case "Non-Query"
cmdOLEDB.ExecuteNonQuery()
DatabaseConnectioninsert = "Non-Query"
End Select
Exit Function
Err:
MsgBox("Database connection error.")
DatabaseConnectioninsert = "Error"
End Function
I am currently trying to insert ~4500 records into the Access Database for each Parameter which takes ~3minutes. However when the project goes live it will have to deal with over 100000 records per Parameter so it is no where near fast enough.
To solve this issue I am thinking of either updating my code to .net or creating a record set, so I can move all of the data in Access at once. Can anyone give me some advice as to which will have the greatest impact to improving the speed of the inserts. I am running visual studio 2005 and Access 2007, updating the database to 2007 rather than compatibility mode is possible but not ideal , however my current code can't access it.
Thank you for your help
Josh
As ridiculous as it sounds, the very best performance you will get on an Access database is using the ancient DAO COM library. Use a RecordSet object to add the records one at a time in a loop and reference the fields by their index (ordinal position) rather than their names. You will find it much, much quicker than using oleDB.ExecuteNonQuery.
See the solution given here for more information. It's C# but it's easy enough to follow and convert to VB.NET if you want to try it out.
Edit
In deference to Remou's comments below: it would appear that Microsoft have in fact been keeping DAO technology up to date – in spite of declaring it obsolete back in 2002 – but you have to use the Office Access Redistributable rather than the better known DAO 3.6 library.