Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help - Type declaration

Status
Not open for further replies.

tdfreeman

MIS
Mar 28, 2002
85
US
I have posted a question about user-defined types and I want to address the question a different way so I am submitting this post.

If I declare a user-defined type like follows:

Public Type Input_Header
RecType As String * 3
HeaderDate As String * 8
FileName As String * 44
End Type

Can I create a 55 character or less string and assign it to a Input_Header variable that I instantiate?

In otherwords, can I do the following?

Dim ihHeader As Input_Header
Dim strHeader As String

strHeader = "000121603extractbilling121603.txt"
ihHeader = strHeader

Thank you for your help.

Tammy

Thank you for your help.

Tammy
 
For the second line of code, it would be:

ihHeader.FileName = strHeader

Also, why would you declare your HeaderDate as string? It would be better to have it declared as Date. Different parts of the world uses different orders of the date. Examples:

December 8, 2003

UK Method

08122003

US Method

12082003

Auguest 12, 2003

UK Method

12082003

US Method

08122003

Do you see where this can be confusing and misleading? By declaring the HeaderDate as Date, you can prevent this issue and allow the code to use the Format Function to do it's task to get it in the proper format, such as:

Dim UKDate as String, USDate as String

UKDate = Format(ihHeader.HeaderDate,"dd/mm/yyyy")
USDate = Format(ihHeader.HeaderDate,"mm/dd/yyyy")

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
When the going gets tough, the tough gets going.
 
If you could do what you are suggesting you'd end up with

ihHeader.RecType (As String * 3) = 000
ihHeader.HeaderDate (As String * 8) = 121603ex
ihHeader.FileName (As String * 44) = tractbilling121603.txt

That should give you a pretty clear idea about why you can't do it.





G LS
spsinkNOJUNK@yahoo.co.uk
Remove the NOJUNK to use.
 
LittleSmudge, sorry that doesn't help. Yes it would end up as you said (I messed up with the string, an 8 character Header Date should be 12062003). I don't understand why it gives me a "preety clear idea about why you can't do it".

rdodge, what I am trying to do is read in a line from a file. It is composed of several fields, all fixed-length. I want to read it into this type variable so that I don't have to assign each field individually. I have 15 different record types, so the rectype from the first three characters determines the layout of the rest of the line and therefore the type I would use if I could get this to work.

I am getting the impression this can't be done, but if anyone can explain to me why it can't I would appreciate it.


Thanks again for your help.

Thank you for your help.

Tammy
 
why not do something like:

ihHeader.RecType (As String * 3) = VBA.Strings.Left(Text,3)
ihHeader.HeaderDate (As String * 8) = VBA.Strings.Mid(Text,4,8)
ihHeader.FileName (As String * 44) = VBA.Strings.Mid(Text,12,44)

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
When the going gets tough, the tough gets going.
 
I don't think you can do what you are trying to do in vba. I've never seen a case/example in vba where you can lay multiple defintions on a give storage area like with lower level languages.

The Mid function isn't that bad and you will probably end up with almost the same amount of code. Keying off the RecType you could have a function for each record type or a Select statement.

Select RecType
Case 1
var1 = Mid(varline,1,3)
etc..
Case 2
var1 = Mid(varline,1,3)
etc..
Case "HDN1 "
var1 = Mid(varline,1,3)
etc..
Case Else
default stuff
End Select
 
cmmrfrds, thanks for the input. Yeah, your idea was and is what I am going to do now that I find that there is no way to do what I was planning.

By saying "I've never seen a case/example in vba where you can lay multiple defintions on a give storage area like with lower level languages." you helped me understand that this is a function that is common with lower level languages and not higher level languages. It is nice to know that someone understands what I was trying to say.

Thanks,

Tammy

Thank you for your help.

Tammy
 
Understandable, it's just like me, I wanted to have key codes to specific keys, but a few of the Virtual Keycodes applies to mulitple keys on the keyboard, and the only way to get to the specific scan codes would be to use lower level languages like C Programming.

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
When the going gets tough, the tough gets going.
 
actually I think I have done something in the lines of what you are describing :

Type RecHeader
rec_id As String * 1
rec_whs As String * 3
rec_date As String * 8
rec_period As String * 6
rec_creation As String * 12
End Type
Dim InputHeader As RecHeader
Dim str_whs As String
Dim str_per As String

Open strFile For Random As intFileNum Len = Len(InputHeader) + Len(vbCrLf)
Get intFileNum, intRecNr, InputHeader
str_per = InputHeader.rec_period
str_whs = InputHeader.rec_whs
Close intFileNum
 
Wow, If I understand what everyone is say, there are separate issues being discussed.

tdfreeman - It looks like what you want to do is to make an assignment to a UDT (User-Defined Type) and have it automatically populate all of the appropriate fields. Since the fields inside a UDT are contiguous, this can be done, but only under some very specific conditions, by using the CopyMemory system call. One of the restrictions is that the individual fields of a UDT must be sized so they line of on half-word boundaries (every two bytes), which, unfortunately, your UDT does not. Your field
Code:
RecType As String * 3
will actually occupy 4 bytes of memory, which eliminates this as a possibility. Also, the CopyMemory will not perform type coercions.

cmmrfrds - I think you are referring to a Union type, where the same physical blocks of memory can be addressed by different variables of different types. Like you, I haven't found anything workable for a Union, and I have tried. (side note: personally, I wouldn't call C and C++ a lower level languages as most people consider VB, C, C++, Pascal, Algol, etc, all as 3rd generation languages)

rdodge - This sounds like an interesting problem. If you're willing, I'd like to try and work with you on the scan codes and see if there is a way to flesh that out.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Me going for the scan codes was mainly for my general level data validation checks as I have created my own custom validation check codes, 2 different levels: General, and Specific, for each field. I do use form level checks too, but that is handled via command buttons.

I talked with a MS Tech person and he basically said there is no way it could be done from VBA cause it's a high level language. If I wanted more control, I would have to use a lower level language (As C programming is lower than VBA). What I did after that, I created an Excel worksheet and showed what each of the different KeyCode/Shift combination codes represented. I also highlighted the different key codes in different colors to indicate what they each meant. The solution I came up with is as follows:

Within Form Module by control:

Private Sub tbxRRC_KeyDown(KeyCode As Integer, Shift As Integer)
KeyCode = modKeyCk.fncKeyLmt(1, KeyCode, Shift, Me.tbxRRC, Me.ldsSB)
End Sub

In this code, it calls on a centralized procedures with the following arguments as shown above:

Argument 1 = Type of Data (Have a total of 7 data types)

Combination of Arguments 2 and 3 = Indicate which key(s) was/were pressed (in some cases, it could be one of 2 keys like the keycode value of 13 could be either enter key even though the numpad keycode is suppose to have the keycode value of 108, but this value is not available to high level programming languages)

Argument 4 = Which control is being checked as data is being entered via the keyboard.

Argument 5 = Which control on the form is the Status bar (Actually a label, but treated as a status bar) to provide the error message, should there be an error.

This is the general level data validation check as it is checking while the user is inputting the data. I have tested the code out and it does work flawlessly (at least of what I could tell) Why I wanted to use the scan codes, there may be times I wanted to setup the 2 different keys to have different meanings, but given the nature of the beast, I had to find some other way, so I just made due with what I could. There is a work around posted for VB, but it doesn't work in VBA.

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
When the going gets tough, the tough gets going.
 
<aside>
I'm not surprised that an MS Tech would think C is a lower level language then VBA, but then it's nothing new for MS to try and redefine things for their own purposes. I wonder what the paradim they use for judging the language level. The industry standard paradim, that being the language generation, was established before Microsoft was even a corporation.
</aside>
Have you tried playing with the GetAsyncKeyState or the GetKeyState API? Can you post a link to the VB workaround, and do you know why the work around does not work in VBA?

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Here's the links that I have attempted:



and the lParam property in Access 2000 VBA did not differentiate between the 2 keys. These links are dealing with VB6, which came out about the same time as Office 2000. Given there are differences between VB6 and Office 2000 VBA, it doesn't surprise me that it didn't work. I also want to say that I was getting some sort of an error with regards to the PeekMessage function, but it's been a long while since I have worked with it.

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
When the going gets tough, the tough gets going.
 
A major issue with most I/O operations is simply the 'speed', as the common approach is to use &quot;Line Input&quot; or similar &quot;record&quot; orientated processes to get a (small) part of a file into memory and process it. A much more efficient I/O process is to read hte entire file into memory in a single 'operation' and deal with the records from the perspective of 'memory'. This has been discussed (and illustrated) several times with various routines, including &quot;basGrabFile&quot;, which should be found using search and the routine name as the ketword. Once the file is in memory, is can easily be parsed (split) into individual records, based on the cr/lf. The individual records may then be processed according to whatever procedures are required.

As far as using a UDT to assign the content to a &quot;Record&quot; (e.g. a table), I am not aware of any automatic process, so wheather the UDT can be filled with some assignment statement(s) appears to me to be irrelevant.



MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Thanks for the links - something to play with.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
CajunCenturion, I agree that C and VB are 3rd generation languages by definition, but I don't agree that most people consider them 3gl's. Many Cobol people I know consider C a lower level language, maybe because it is more cryptic and you need to write your own code to do things that are built into Cobol. Also, a lot of Access Programmers I know that write in vba consider C or Cobol a lower level language. I have written in many of these languages including lots of 4gls and have generally found that people look at C as a lower level language than a language like VB. Anyway, that is my 2 cents - I kind of go with what I think communicates best at the time.
 
I do understand what you're saying, and as far as &quot;most people&quot; are concerned, I guess it's largely dependant upon your circle of associates, and their backgrounds.

I guess the basic question that I would ask, is what standard are going to use to judge how high or low a language is? I have written more programs in COBOL than I care to admit, and if you use the criteria of what is built into the language, then VBA is a lower level language because there are things built into COBOL that VBA can only dream about having. In fact, there is very little built directly into VBA, although it appears otherwise because the interfaces are transparent. C certainly can be cryptic, if you write it that way, but the same arguement can be made for Perl. To me, I consider that a programmer style issue, and not an inherant property of the language.

Just for the fun of it, if you're not going to use the standard language generation criteria, then what makes a language a low-level language, and converserly, what makes a language a high level language?

Obviously there is no right or wrong, just a discussion for fun.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I can certainly understand both of ya's points. One other thing that I have noticed (at least how MS seems to use it), the lower the level, the more you can do with it, but the more cryptic and technical one may have to be with the language. I must admit I have not programmed with C, but with what I have seen of it, it doesn't appear to be all that hard to learn. I guess that's cause I have been doing programming work for the last few years despite the fact I'm not even in the IT department.

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
When the going gets tough, the tough gets going.
 
We hashed all this out in the other post. You can't assign a variant string to a UDT. VBA itself isn't coded to handle it.

On the language question, an old boss of mine always said that C was a &quot;fourth generation assembly language&quot;, which is an interesting take. Myself, I always thought that procedural languages were 3rd gen and object-oriented were 4th, so C, Cobol would be 3rd, while VB, VBA, C++ were 4th.
The object layers are the &quot;higher level&quot;
 
That is quite reasonable vbajock, and is consistent with the standard generational definitions. Only problem I see is that VB and VBA are not object oriented. They are &quot;object friendly&quot;, in that they can make use of objects. But without inheritance and polymorphism, two of the fundamental precepts of objects, one has to question of whether or not such languages are truly object oriented. No question about C++, or any of the .Net family as being object oriented.

I've heard arguments that even OO languages are still 3rd generation because at their core, they are still procedure (method) oriented. Even events are simply procedures, although the trigger mecahism is not procedural.

SQL has some interesting properties, such as database agnosticism, and cross platform utility, and independance from an underlying procedural language which might qualify it as a 4th generation langugage.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top